Skip to content
This repository has been archived by the owner on Jun 3, 2022. It is now read-only.

Commit

Permalink
feat(module.defid): Detect tip stale by height (#975)
Browse files Browse the repository at this point in the history
* check height after 90 mins

* revert unuse change

* move stale detection logic to model probe

* revert defid probe

* miss set index = highest.height

* pr req & add test

* add restore mock time

* minor changes
  • Loading branch information
canonbrother authored May 23, 2022
1 parent 1896b7c commit 66fb403
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 2 deletions.
42 changes: 42 additions & 0 deletions src/module.api/actuator.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,4 +200,46 @@ describe('with peers', () => {
status: 'ok'
})
})

it('/_actuator/probes/readiness stale', async () => {
// Time travel 100 mins
const mockTime = jest.spyOn(Date, 'now').mockImplementation(() => new Date().getTime() + 6_000_000)

const res = await app.inject({
method: 'GET',
url: '/_actuator/probes/readiness'
})
expect(res.statusCode).toStrictEqual(503)
expect(res.json()).toStrictEqual({
details: {
defid: {
blocks: expect.any(Number),
headers: expect.any(Number),
initialBlockDownload: false,
peers: 1,
status: 'up'
},
model: {
status: 'down'
}
},
error: {
model: {
status: 'down'
}
},
info: {
defid: {
blocks: expect.any(Number),
headers: expect.any(Number),
initialBlockDownload: false,
peers: 1,
status: 'up'
}
},
status: 'error'
})

mockTime.mockRestore()
})
})
16 changes: 14 additions & 2 deletions src/module.model/_model.probes.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ProbeIndicator } from '@src/module.health/probe.indicator'
import { Injectable } from '@nestjs/common'
import { HealthIndicatorResult } from '@nestjs/terminus'
import { BlockMapper } from '@src/module.model/block'
import { Block, BlockMapper } from '@src/module.model/block'
import { JsonRpcClient } from '@defichain/jellyfish-api-jsonrpc'

@Injectable()
Expand Down Expand Up @@ -32,13 +32,16 @@ export class ModelProbeIndicator extends ProbeIndicator {
* - unable to get the latest block
* - synced blocks are undefined
* - synced blocks are more than 2 blocks behind
* - synced latest block height is not more than 90 mins old
*/
async readiness (): Promise<HealthIndicatorResult> {
let highest: Block
let index: number | undefined
let defid: number | undefined

try {
index = (await this.block.getHighest())?.height
highest = await this.block.getHighest() as Block
index = highest.height
defid = await this.client.blockchain.getBlockCount()
} catch (err) {
return this.withDead('model', 'unable to get the latest block')
Expand All @@ -59,6 +62,15 @@ export class ModelProbeIndicator extends ProbeIndicator {
return this.withDead('model', 'synced blocks are more than 2 blocks behind', details)
}

// index defid can experience rollback, so make sure the condition is only checked if `Model == DeFid`
if (index === defid && secondsSince(highest.time) >= 90 * 60) {
return this.withDead('model', 'defid chain is stale')
}

return this.withAlive('model', details)
}
}

function secondsSince (timeInSeconds: number): number {
return (Math.floor(Date.now() / 1000)) - timeInSeconds
}

0 comments on commit 66fb403

Please sign in to comment.