Skip to content

Commit

Permalink
Support multichain historical
Browse files Browse the repository at this point in the history
  • Loading branch information
stwiname committed Nov 11, 2024
1 parent 106a35b commit 013a6f4
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 11 deletions.
8 changes: 4 additions & 4 deletions packages/node-core/src/indexer/store.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -310,16 +310,16 @@ export class StoreService {

if (value && multiChain) {
throw new Error(
'Historical feature is enabled and not compatible with multi-chain, to multi-chain index clear postgres schema and re-index project using --multichain'
'Historical indexing by height is enabled and not compatible with multi-chain, to multi-chain index clear postgres schema and re-index project using --multichain'
);
}

// TODO parse through CLI/Project option and consider multichain
return value ? 'height' : false;
} catch (e) {
if (multiChain && historical) {
logger.info('Historical state is not compatible with multi chain indexing, disabling historical..');
return false;
if (multiChain && historical === 'height') {
logger.warn('Historical state by height is not compatible with multi chain indexing, using timestamp instead.');
return 'timestamp';
}
// Will trigger on first startup as metadata table doesn't exist
// Default fallback to "height" for backwards compatible
Expand Down
1 change: 1 addition & 0 deletions packages/node-core/src/yargs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ export function yargsBuilder<
choices: ['fatal', 'error', 'warn', 'info', 'debug', 'trace', 'silent'],
},
'multi-chain': {
alias: 'multichain',
demandOption: false,
default: false,
describe: 'Enables indexing multiple subquery projects into the same database schema',
Expand Down
6 changes: 4 additions & 2 deletions packages/query/src/graphql/plugins/GetMetadataPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const {version: packageVersion} = require('../../../package.json');
const META_JSON_FIELDS = ['deployments'];
const METADATA_TYPES = {
lastProcessedHeight: 'number',
lastProcessedBlockTimestamp: 'number',
lastProcessedTimestamp: 'number',
targetHeight: 'number',
lastFinalizedVerifiedHeight: 'number',
Expand All @@ -34,6 +35,7 @@ const METADATA_TYPES = {
lastCreatedPoiHeight: 'number',
latestSyncedPoiHeight: 'number',
dbSize: 'string',
historicalStateEnabled: 'string',
};

const METADATA_KEYS = Object.keys(METADATA_TYPES);
Expand Down Expand Up @@ -132,7 +134,7 @@ async function fetchMetadataFromTable(
// Store default metadata name in table avoid query system table
let defaultMetadataName: string;

async function fetchFromTable(
export async function fetchFromTable(
pgClient: Client,
schemaName: string,
chainId: string | undefined,
Expand Down Expand Up @@ -239,8 +241,8 @@ export const GetMetadataPlugin = makeExtendSchemaPlugin((build: Build, options)
_metadatas(
after: Cursor
before: Cursor # distinct: [_mmr_distinct_enum] = null # filter: _MetadataFilter # first: Int # offset: Int
# orderBy: [_MetadatasOrderBy!] = [PRIMARY_KEY_ASC]
): # last: Int
# orderBy: [_MetadatasOrderBy!] = [PRIMARY_KEY_ASC]
_Metadatas
}
`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import {QueryBuilder} from '@subql/x-graphile-build-pg';
import {Plugin, Context} from 'graphile-build';
import {GraphQLString} from 'graphql';
import {fetchFromTable} from '../GetMetadataPlugin';
import {makeRangeQuery, hasBlockRange} from './utils';

function addRangeQuery(queryBuilder: QueryBuilder, sql: any) {
Expand All @@ -18,13 +19,17 @@ function addQueryContext(queryBuilder: QueryBuilder, sql: any, blockHeight: any)
}

export const PgBlockHeightPlugin: Plugin = async (builder, options) => {
// Note this varies from node where true is allowed because of legacy support
let historicalMode: boolean | 'height' | 'timestamp' = 'height';
const [schemaName] = options.pgSchemas;
const {rows} = await options.pgConfig.query(
`SELECT * FROM "${schemaName}"._metadata WHERE key = 'historicalStateEnabled'`
);

// Note this varies from node where true is allowed because of legacy support
const historicalMode = rows[0]?.value || ('block' as boolean | 'block' | 'timestamp');
try {
const {historicalStateEnabled} = await fetchFromTable(options.pgConfig, schemaName, undefined, false);
historicalMode = historicalStateEnabled;
} catch (e) {
/* Do nothing, default value is already set */
}

// Adds blockHeight condition to join clause when joining a table that has _block_range column
builder.hook(
'GraphQLObjectType:fields:field',
Expand Down
2 changes: 2 additions & 0 deletions packages/utils/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Changed
- Update metadata types (#2584)

## [2.14.0] - 2024-08-05
### Added
Expand Down
2 changes: 2 additions & 0 deletions packages/utils/src/query/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export type TableEstimate = {

export type MetaData = {
lastProcessedHeight: number;
lastProcessedBlockTimestamp: number;
lastProcessedTimestamp: number;
targetHeight: number;
chain: string;
Expand All @@ -19,4 +20,5 @@ export type MetaData = {
startHeight?: number;
rowCountEstimate: TableEstimate[];
deployments: Record<number, string>;
historicalStateEnabled: boolean | 'height' | 'timestamp';
};

0 comments on commit 013a6f4

Please sign in to comment.