Skip to content

Commit 3dff433

Browse files
authored
Rework poi (#1966)
* migration poi creation add indexes tidy up fix index query tidy up logger, workers and reindex clean mmr clean mmr test and add validation remove logs drop mmr table fix up fix up test Update packages/common/src/constants.ts Co-authored-by: Scott Twiname <[email protected]> Update packages/node-core/src/indexer/poi/poi.service.ts Co-authored-by: Scott Twiname <[email protected]> Update packages/node-core/src/indexer/poi/poi.service.ts Co-authored-by: Scott Twiname <[email protected]> Update packages/node-core/src/indexer/poi/poi.service.ts Co-authored-by: Scott Twiname <[email protected]> Update packages/node-core/src/indexer/poi/poi.service.ts Co-authored-by: Scott Twiname <[email protected]> catch error and exit fix add more test * fix * lint * fix test * update * comment * use injection * private
1 parent 01a5dc4 commit 3dff433

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+744
-2566
lines changed

packages/common/src/constants.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,8 @@ export const IPFS_REGEX = /^ipfs:\/\//i;
1414
// MANIFEST
1515
export const RUNNER_REGEX = /(\^?)(\d|x|\*)+\.(\d|x|\*)+\.(\d|x|\*)+/;
1616

17-
//MMR
18-
export const DEFAULT_WORD_SIZE = 32;
19-
export const DEFAULT_LEAF = Buffer.from('0000000000000000000000000000000000000000000000000000000000000001', 'hex');
20-
export const MMR_AWAIT_TIME = 2;
17+
// POI
18+
export const POI_AWAIT_TIME = 2; // seconds
2119

2220
// NETWORK
2321
export enum NETWORK_FAMILY {

packages/node-core/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
"@subql/testing": "workspace:*",
2828
"@subql/types": "workspace:*",
2929
"@subql/utils": "workspace:*",
30-
"@subql/x-merkle-mountain-range": "^2.0.0-0.1.3",
3130
"@subql/x-sequelize": "6.32.0-0.0.2",
3231
"@willsoto/nestjs-prometheus": "^5.1.1",
3332
"async-lock": "^1.4.0",

packages/node-core/src/configure/NodeConfig.ts

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,6 @@ import {assign} from '../utils/object';
1212

1313
const logger = getLogger('configure');
1414

15-
export enum MmrStoreType {
16-
File = 'file',
17-
Postgres = 'postgres',
18-
}
19-
2015
export interface IConfig {
2116
readonly subquery: string;
2217
readonly subqueryName?: string;
@@ -36,8 +31,6 @@ export interface IConfig {
3631
readonly indexCountLimit: number;
3732
readonly timestampField: boolean;
3833
readonly proofOfIndex: boolean;
39-
readonly mmrStoreType: MmrStoreType;
40-
readonly mmrPath?: string;
4134
readonly ipfs?: string;
4235
readonly dictionaryTimeout: number;
4336
readonly workers?: number;
@@ -75,7 +68,6 @@ const DEFAULT_CONFIG = {
7568
indexCountLimit: 10,
7669
timestampField: true,
7770
proofOfIndex: false,
78-
mmrStoreType: MmrStoreType.Postgres,
7971
dictionaryTimeout: 30,
8072
profiler: false,
8173
subscription: false,
@@ -219,17 +211,10 @@ export class NodeConfig implements IConfig {
219211
return this._config.proofOfIndex;
220212
}
221213

222-
get mmrStoreType(): MmrStoreType {
223-
return this._config.mmrStoreType;
224-
}
225-
226214
get dictionaryTimeout(): number {
227215
return this._config.dictionaryTimeout;
228216
}
229217

230-
get mmrPath(): string {
231-
return this._config.mmrPath ?? `.mmr/${this.subqueryName}.mmr`;
232-
}
233218
get ipfs(): string | undefined {
234219
return this._config.ipfs;
235220
}

packages/node-core/src/events.ts

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export enum IndexerEvent {
1818
}
1919

2020
export enum PoiEvent {
21-
LastPoiWithMmr = 'last_poi_with_mmr',
21+
LatestSyncedPoi = 'poi_synced',
2222
PoiTarget = 'poi_target',
2323
}
2424

@@ -48,21 +48,3 @@ export interface NetworkMetadataPayload {
4848
specName: string;
4949
genesisHash: string;
5050
}
51-
52-
export interface MmrPayload {
53-
offset: number;
54-
height: number;
55-
hash: string; //the node hash
56-
mmrRoot: string;
57-
}
58-
59-
export interface MmrProof {
60-
digest: string;
61-
leafLength: number;
62-
nodes: MmrNode[];
63-
}
64-
65-
export interface MmrNode {
66-
node: string;
67-
hash: string;
68-
}

packages/node-core/src/indexer/benchmark.service.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,8 @@ export class PoiBenchmarkService extends BaseBenchmarkService {
104104
}
105105
}
106106

107-
@OnEvent(PoiEvent.LastPoiWithMmr)
108-
handleLastPoiWithMmr(blockPayload: ProcessBlockPayload): void {
107+
@OnEvent(PoiEvent.LatestSyncedPoi)
108+
handleLatestSyncedPoi(blockPayload: ProcessBlockPayload): void {
109109
this.currentProcessingHeight = blockPayload.height;
110110
this.currentProcessingTimestamp = blockPayload.timestamp;
111111
}

packages/node-core/src/indexer/blockDispatcher/base-block-dispatcher.ts

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -167,22 +167,15 @@ export abstract class BaseBlockDispatcher<Q extends IQueue, DS> implements IBloc
167167
const operationHash = this.storeService.getOperationMerkleRoot();
168168
const {blockHash, dynamicDsCreated, reindexBlockHeight} = processBlockResponse;
169169

170-
await this.updatePOI(height, blockHash, operationHash);
170+
this.createPOI(height, blockHash, operationHash);
171171

172172
if (reindexBlockHeight !== null && reindexBlockHeight !== undefined) {
173173
await this.rewind(reindexBlockHeight);
174174
this.setLatestProcessedHeight(reindexBlockHeight);
175175
} else {
176176
this.updateStoreMetadata(height);
177177
if (this.nodeConfig.proofOfIndex && !isNullMerkelRoot(operationHash)) {
178-
// We only check if it is undefined, need to be caution here when blockOffset is 0
179-
if (this.projectService.blockOffset === undefined) {
180-
// Which means during project init, it has not found offset and set value
181-
this.storeCacheService.metadata.set('blockOffset', height - 1);
182-
}
183-
// this will return if project service blockOffset already exist
184-
// dont await this, this starts a loop
185-
void this.projectService.setBlockOffset(height - 1);
178+
await this.poiService.ensureGenesisPoi(height);
186179
}
187180
if (dynamicDsCreated) {
188181
await this.onDynamicDsCreated(height);
@@ -208,27 +201,20 @@ export abstract class BaseBlockDispatcher<Q extends IQueue, DS> implements IBloc
208201
}
209202
}
210203

211-
private async updatePOI(height: number, blockHash: string, operationHash: Uint8Array): Promise<void> {
204+
// First creation of POI
205+
private createPOI(height: number, blockHash: string, operationHash: Uint8Array): void {
212206
if (!this.nodeConfig.proofOfIndex) {
213207
return;
214208
}
215-
//check if operation is null, then poi will not be inserted
216209
if (!u8aEq(operationHash, NULL_MERKEL_ROOT)) {
217-
const poiBlock = PoiBlock.create(
218-
height,
219-
blockHash,
220-
operationHash,
221-
await this.poiService.getLatestPoiBlockHash(),
222-
this.project.id
223-
);
210+
const poiBlock = PoiBlock.create(height, blockHash, operationHash, this.project.id);
224211
// This is the first creation of POI
225212
this.poi.bulkUpsert([poiBlock]);
213+
this.storeCacheService.metadata.setBulk([{key: 'lastCreatedPoiHeight', value: height}]);
226214
this.eventEmitter.emit(PoiEvent.PoiTarget, {
227215
height,
228216
timestamp: Date.now(),
229217
});
230-
this.poiService.setLatestPoiBlockHash(poiBlock.hash);
231-
this.storeCacheService.metadata.set('lastPoiHeight', height);
232218
}
233219
}
234220

@@ -248,7 +234,7 @@ export abstract class BaseBlockDispatcher<Q extends IQueue, DS> implements IBloc
248234
private get poi(): CachePoiModel {
249235
const poi = this.storeCacheService.poi;
250236
if (!poi) {
251-
throw new Error('MMR service expected POI but it was not found');
237+
throw new Error('Poi service expected poi repo but it was not found');
252238
}
253239
return poi;
254240
}

packages/node-core/src/indexer/entities/Metadata.entity.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,17 @@ export interface MetadataKeys {
1919
runnerQuery: string;
2020
runnerQueryVersion: string;
2121
specName: string;
22-
lastPoiHeight: number;
2322
lastFinalizedVerifiedHeight: number;
2423
indexerHealthy: boolean;
2524
targetHeight: number;
2625
dynamicDatasources: string;
2726
unfinalizedBlocks: string;
2827
schemaMigrationCount: number;
2928
deployments: string;
30-
latestPoiWithMmr: string;
29+
lastCreatedPoiHeight: number;
30+
latestSyncedPoiHeight: number;
31+
latestPoiWithMmr: string; // Deprecated, keep for poi migration
32+
lastPoiHeight: string; // Deprecated, keep for poi migration
3133
}
3234

3335
export interface Metadata {

packages/node-core/src/indexer/entities/Mmr.entitiy.ts

Lines changed: 0 additions & 121 deletions
This file was deleted.

packages/node-core/src/indexer/entities/Poi.entity.ts

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
11
// Copyright 2020-2023 SubQuery Pte Ltd authors & contributors
22
// SPDX-License-Identifier: GPL-3.0
33

4-
import {BuildOptions, DataTypes, Model, Sequelize} from '@subql/x-sequelize';
4+
import {BuildOptions, DataTypes, Model, Op, Sequelize} from '@subql/x-sequelize';
55

66
export interface ProofOfIndex {
77
id: number; //blockHeight
8-
chainBlockHash: Uint8Array;
9-
hash: Uint8Array;
10-
parentHash?: Uint8Array;
11-
operationHashRoot: Uint8Array;
12-
mmrRoot?: Uint8Array;
8+
chainBlockHash: Uint8Array | null;
9+
hash: Uint8Array | undefined;
10+
parentHash: Uint8Array | undefined;
11+
operationHashRoot: Uint8Array | null;
1312
projectId?: string;
1413
}
1514

15+
export interface SyncedProofOfIndex extends ProofOfIndex {
16+
hash: Uint8Array;
17+
parentHash: Uint8Array;
18+
}
19+
1620
export interface PoiModel extends Model<ProofOfIndex>, ProofOfIndex {}
1721

1822
export type PoiRepo = typeof Model & {
@@ -46,11 +50,6 @@ export function PoiFactoryDeprecate(sequelize: Sequelize, schema: string): PoiRe
4650
type: DataTypes.BLOB,
4751
allowNull: false,
4852
},
49-
mmrRoot: {
50-
type: DataTypes.BLOB,
51-
allowNull: true,
52-
unique: true,
53-
},
5453
projectId: {
5554
type: DataTypes.STRING,
5655
allowNull: false,
@@ -75,33 +74,27 @@ export function PoiFactory(sequelize: Sequelize, schema: string): PoiRepo {
7574
},
7675
chainBlockHash: {
7776
type: DataTypes.BLOB,
78-
allowNull: false,
77+
allowNull: true,
7978
unique: true,
8079
},
8180
hash: {
8281
type: DataTypes.BLOB,
83-
allowNull: false,
82+
allowNull: true,
8483
unique: true,
8584
},
8685
parentHash: {
8786
type: DataTypes.BLOB,
88-
allowNull: false,
87+
allowNull: true,
8988
unique: true,
9089
},
9190
operationHashRoot: {
92-
type: DataTypes.BLOB,
93-
allowNull: false,
94-
},
95-
mmrRoot: {
9691
type: DataTypes.BLOB,
9792
allowNull: true,
98-
unique: true,
9993
},
10094
},
10195
{
10296
freezeTableName: true,
10397
schema: schema,
104-
indexes: [{fields: ['hash']}],
10598
}
10699
);
107100
}

0 commit comments

Comments
 (0)