Skip to content

Commit

Permalink
fix(whale-api): add reindex for failed to indexed block (#2169)
Browse files Browse the repository at this point in the history
<!--  Thanks for sending a pull request! -->

#### What this PR does / why we need it:

Fixing random bug `!bestChain` 

- introduce status `reindex` to jump off the cleanup cycle if
`NotFoundIndexError`
- logged on random bug`!bestChain`

#### Which issue(s) does this PR fixes?:
<!--
(Optional) Automatically closes linked issue when PR is merged.
Usage: `Fixes #<issue number>`, or `Fixes (paste link of issue)`.
-->
Fixes #

#### Additional comments?:
  • Loading branch information
lykalabrada authored Nov 10, 2023
1 parent 6f312b9 commit dd01b74
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
11 changes: 9 additions & 2 deletions apps/whale-api/src/module.indexer/rpc.block.provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Block, BlockMapper } from '../module.model/block'
import { IndexStatusMapper, Status } from './status'
import { waitForCondition } from '@defichain/testcontainers/dist/utils'
import { blockchain as defid, RpcApiError } from '@defichain/jellyfish-api-core'
import { NotFoundIndexerError } from './error'

@Injectable()
export class RPCBlockProvider {
Expand Down Expand Up @@ -90,6 +91,7 @@ export class RPCBlockProvider {
if (await RPCBlockProvider.isBestChain(indexed, nextBlock)) {
await this.index(nextBlock)
} else {
this.logger.error(`indexedBlock{height: ${indexed.height}, hash: ${indexed.hash}}, nextBlock{height: ${nextBlock.height}, prevHash: ${nextBlock.previousblockhash}}`)
await this.invalidate(indexed.hash, indexed.height)
}
return true
Expand All @@ -100,7 +102,7 @@ export class RPCBlockProvider {
* @param {defid.Block<Transaction>} nextBlock to check previous block hash
*/
private static async isBestChain (indexed: Block, nextBlock: defid.Block<defid.Transaction>): Promise<boolean> {
return nextBlock.previousblockhash === indexed.hash
return indexed.hash === nextBlock.previousblockhash
}

public async indexGenesis (): Promise<boolean> {
Expand All @@ -121,6 +123,7 @@ export class RPCBlockProvider {
switch (status.status) {
case Status.INVALIDATED:
case Status.INDEXED:
case Status.REINDEX:
return
}

Expand Down Expand Up @@ -149,7 +152,11 @@ export class RPCBlockProvider {
await this.indexer.invalidate(hash)
await this.statusMapper.put(hash, height, Status.INVALIDATED)
} catch (err) {
await this.statusMapper.put(hash, height, Status.ERROR)
if (err instanceof NotFoundIndexerError) {
await this.statusMapper.put(hash, height, Status.REINDEX)
} else {
await this.statusMapper.put(hash, height, Status.ERROR)
}
throw err
}
}
Expand Down
3 changes: 2 additions & 1 deletion apps/whale-api/src/module.indexer/status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ export enum Status {
INDEXED = 'INDEXED',
INVALIDATING = 'INVALIDATING',
INVALIDATED = 'INVALIDATED',
ERROR = 'ERROR'
ERROR = 'ERROR',
REINDEX = 'REINDEX'
}

export interface IndexStatus extends Model {
Expand Down

0 comments on commit dd01b74

Please sign in to comment.