Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(whale-api): add reindex for failed to indexed block #2169

Merged
merged 5 commits into from
Nov 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 { 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 @@
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 @@
* @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
Copy link
Contributor

@canonbrother canonbrother Nov 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice 🤓 !! (lesser brain consuming)

}

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

Expand Down Expand Up @@ -149,7 +152,11 @@
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)

Check warning on line 158 in apps/whale-api/src/module.indexer/rpc.block.provider.ts

View check run for this annotation

Codecov / codecov/patch

apps/whale-api/src/module.indexer/rpc.block.provider.ts#L156-L158

Added lines #L156 - L158 were not covered by tests
}
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