mirror of
https://github.com/cerc-io/watcher-ts
synced 2024-11-19 20:36:19 +00:00
Fix pruning of canonical block if null block is encountered (#499)
This commit is contained in:
parent
a6deed9c27
commit
33e4455f92
@ -324,8 +324,8 @@ export class Database implements DatabaseInterface {
|
||||
await this._baseDatabase.deleteEntitiesByConditions(queryRunner, entity, findConditions);
|
||||
}
|
||||
|
||||
async getAncestorAtDepth (blockHash: string, depth: number): Promise<string> {
|
||||
return this._baseDatabase.getAncestorAtDepth(blockHash, depth);
|
||||
async getAncestorAtHeight (blockHash: string, height: number): Promise<string> {
|
||||
return this._baseDatabase.getAncestorAtHeight(blockHash, height);
|
||||
}
|
||||
|
||||
_getPropertyColumnMapForEntity (entityName: string): Map<string, string> {
|
||||
|
@ -744,8 +744,8 @@ export class Indexer implements IndexerInterface {
|
||||
return this._baseIndexer.updateBlockProgress(block, lastProcessedEventIndex);
|
||||
}
|
||||
|
||||
async getAncestorAtDepth (blockHash: string, depth: number): Promise<string> {
|
||||
return this._baseIndexer.getAncestorAtDepth(blockHash, depth);
|
||||
async getAncestorAtHeight (blockHash: string, height: number): Promise<string> {
|
||||
return this._baseIndexer.getAncestorAtHeight(blockHash, height);
|
||||
}
|
||||
|
||||
async resetWatcherToBlock (blockNumber: number): Promise<void> {
|
||||
|
@ -107,9 +107,9 @@ export class Indexer implements IndexerInterface {
|
||||
return [];
|
||||
}
|
||||
|
||||
async getAncestorAtDepth (blockHash: string, depth: number): Promise<string> {
|
||||
async getAncestorAtHeight (blockHash: string, height: number): Promise<string> {
|
||||
assert(blockHash);
|
||||
assert(depth);
|
||||
assert(height);
|
||||
|
||||
return '';
|
||||
}
|
||||
|
@ -457,15 +457,14 @@ export class Database {
|
||||
await repo.delete(findConditions);
|
||||
}
|
||||
|
||||
async getAncestorAtDepth (blockHash: string, depth: number): Promise<string> {
|
||||
async getAncestorAtHeight (blockHash: string, height: number): Promise<string> {
|
||||
const heirerchicalQuery = `
|
||||
WITH RECURSIVE cte_query AS
|
||||
(
|
||||
SELECT
|
||||
block_hash,
|
||||
block_number,
|
||||
parent_hash,
|
||||
0 as depth
|
||||
parent_hash
|
||||
FROM
|
||||
block_progress
|
||||
WHERE
|
||||
@ -474,14 +473,13 @@ export class Database {
|
||||
SELECT
|
||||
b.block_hash,
|
||||
b.block_number,
|
||||
b.parent_hash,
|
||||
c.depth + 1
|
||||
b.parent_hash
|
||||
FROM
|
||||
block_progress b
|
||||
INNER JOIN
|
||||
cte_query c ON c.parent_hash = b.block_hash
|
||||
WHERE
|
||||
c.depth < $2
|
||||
b.block_number >= $2
|
||||
)
|
||||
SELECT
|
||||
block_hash, block_number
|
||||
@ -492,7 +490,7 @@ export class Database {
|
||||
`;
|
||||
|
||||
// Get ancestor block hash using heirarchical query.
|
||||
const [{ block_hash: ancestorBlockHash }] = await this._conn.query(heirerchicalQuery, [blockHash, depth]);
|
||||
const [{ block_hash: ancestorBlockHash }] = await this._conn.query(heirerchicalQuery, [blockHash, height]);
|
||||
|
||||
return ancestorBlockHash;
|
||||
}
|
||||
|
@ -713,8 +713,8 @@ export class Indexer {
|
||||
}
|
||||
}
|
||||
|
||||
async getAncestorAtDepth (blockHash: string, depth: number): Promise<string> {
|
||||
return this._db.getAncestorAtDepth(blockHash, depth);
|
||||
async getAncestorAtHeight (blockHash: string, height: number): Promise<string> {
|
||||
return this._db.getAncestorAtHeight(blockHash, height);
|
||||
}
|
||||
|
||||
async saveEventEntity (dbEvent: EventInterface): Promise<EventInterface> {
|
||||
|
@ -445,10 +445,18 @@ export class JobRunner {
|
||||
// We have more than one node at this height, so prune all nodes not reachable from indexed block at max reorg depth from prune height.
|
||||
// This will lead to orphaned nodes, which will get pruned at the next height.
|
||||
if (blocksAtHeight.length > 1) {
|
||||
const [indexedBlock] = await this._indexer.getBlocksAtHeight(pruneBlockHeight + MAX_REORG_DEPTH, false);
|
||||
let indexedBlock: BlockProgressInterface | undefined;
|
||||
let indexedBlockHeight = pruneBlockHeight + MAX_REORG_DEPTH;
|
||||
|
||||
// Loop to find latest indexed block incase null block is encountered
|
||||
while (!indexedBlock) {
|
||||
[indexedBlock] = await this._indexer.getBlocksAtHeight(indexedBlockHeight, false);
|
||||
--indexedBlockHeight;
|
||||
assert(indexedBlockHeight > pruneBlockHeight, `No blocks found above pruneBlockHeight ${pruneBlockHeight}`);
|
||||
}
|
||||
|
||||
// Get ancestor blockHash from indexed block at prune height.
|
||||
const ancestorBlockHash = await this._indexer.getAncestorAtDepth(indexedBlock.blockHash, MAX_REORG_DEPTH);
|
||||
const ancestorBlockHash = await this._indexer.getAncestorAtHeight(indexedBlock.blockHash, pruneBlockHeight);
|
||||
newCanonicalBlockHash = ancestorBlockHash;
|
||||
|
||||
const blocksToBePruned = blocksAtHeight.filter(block => ancestorBlockHash !== block.blockHash);
|
||||
|
@ -173,7 +173,7 @@ export interface IndexerInterface {
|
||||
getLatestCanonicalBlock (): Promise<BlockProgressInterface | undefined>
|
||||
getLatestStateIndexedBlock (): Promise<BlockProgressInterface>
|
||||
getBlockEvents (blockHash: string, where: Where, queryOptions: QueryOptions): Promise<Array<EventInterface>>
|
||||
getAncestorAtDepth (blockHash: string, depth: number): Promise<string>
|
||||
getAncestorAtHeight (blockHash: string, height: number): Promise<string>
|
||||
saveBlockAndFetchEvents (block: DeepPartial<BlockProgressInterface>): Promise<[
|
||||
BlockProgressInterface,
|
||||
DeepPartial<EventInterface>[],
|
||||
@ -249,7 +249,7 @@ export interface DatabaseInterface {
|
||||
getBlockEvents (blockHash: string, where?: Where, queryOptions?: QueryOptions): Promise<EventInterface[]>;
|
||||
getEvent (id: string): Promise<EventInterface | undefined>
|
||||
getSyncStatus (queryRunner: QueryRunner): Promise<SyncStatusInterface | undefined>
|
||||
getAncestorAtDepth (blockHash: string, depth: number): Promise<string>
|
||||
getAncestorAtHeight (blockHash: string, height: number): Promise<string>
|
||||
getProcessedBlockCountForRange (fromBlockNumber: number, toBlockNumber: number): Promise<{ expected: number, actual: number }>;
|
||||
getEventsInRange (fromBlockNumber: number, toBlockNumber: number): Promise<Array<EventInterface>>;
|
||||
markBlocksAsPruned (queryRunner: QueryRunner, blocks: BlockProgressInterface[]): Promise<void>;
|
||||
|
Loading…
Reference in New Issue
Block a user