-
Notifications
You must be signed in to change notification settings - Fork 117
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[CT-839] Add blockHeight to subaccount websocket message (#1585)
- Loading branch information
Showing
28 changed files
with
234 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
indexer/packages/postgres/__tests__/loops/block-height-refresher.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { clearData, migrate, teardown } from '../../src/helpers/db-helpers'; | ||
import { clear, getLatestBlockHeight, updateBlockHeight } from '../../src/loops/block-height-refresher'; | ||
import { defaultBlock2 } from '../helpers/constants'; | ||
import { seedData } from '../helpers/mock-generators'; | ||
import config from '../../src/config'; | ||
|
||
describe('blockHeightRefresher', () => { | ||
beforeAll(async () => { | ||
await migrate(); | ||
await seedData(); | ||
await updateBlockHeight(); | ||
}); | ||
|
||
afterAll(async () => { | ||
await clearData(); | ||
await teardown(); | ||
}); | ||
|
||
describe('getLatestBlockHeight', () => { | ||
it('successfully gets the latest block height after update', async () => { | ||
await updateBlockHeight(); | ||
expect(getLatestBlockHeight()).toBe(defaultBlock2.blockHeight); | ||
}); | ||
}); | ||
|
||
describe('clear', () => { | ||
it('throws an error if block height does not exist', () => { | ||
clear(); | ||
expect(() => getLatestBlockHeight()).toThrowError('Unable to find latest block height'); | ||
}); | ||
|
||
it('throws an error when clear is called in non-test environment', () => { | ||
const originalEnv = config.NODE_ENV; | ||
config.NODE_ENV = 'production'; | ||
expect(() => clear()).toThrowError('clear cannot be used in non-test env'); | ||
config.NODE_ENV = originalEnv; | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
indexer/packages/postgres/src/loops/block-height-refresher.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { | ||
stats, | ||
logger, | ||
NodeEnv, | ||
} from '@dydxprotocol-indexer/base'; | ||
|
||
import config from '../config'; | ||
import * as BlockTable from '../stores/block-table'; | ||
import { BlockFromDatabase, Options } from '../types'; | ||
import { startUpdateLoop } from './loopHelper'; | ||
|
||
let latestBlockHeight: string = ''; | ||
|
||
/** | ||
* Refresh loop to cache the latest block height from the database in-memory. | ||
*/ | ||
export async function start(): Promise<void> { | ||
await startUpdateLoop( | ||
updateBlockHeight, | ||
config.BLOCK_HEIGHT_REFRESHER_INTERVAL_MS, | ||
'updateBlockHeight', | ||
); | ||
} | ||
|
||
/** | ||
* Updates in-memory latest block height. | ||
*/ | ||
export async function updateBlockHeight(options?: Options): Promise<void> { | ||
const startTime: number = Date.now(); | ||
try { | ||
const latestBlock: BlockFromDatabase = await BlockTable.getLatest( | ||
options || { readReplica: true }, | ||
); | ||
latestBlockHeight = latestBlock.blockHeight; | ||
stats.timing(`${config.SERVICE_NAME}.loops.update_block_height`, Date.now() - startTime); | ||
// eslint-disable-next-line no-empty | ||
} catch (error) { } | ||
} | ||
|
||
/** | ||
* Gets the latest block height. | ||
*/ | ||
export function getLatestBlockHeight(): string { | ||
if (!latestBlockHeight) { | ||
const message: string = 'Unable to find latest block height'; | ||
logger.error({ | ||
at: 'block-height-refresher#getLatestBlockHeight', | ||
message, | ||
}); | ||
throw new Error(message); | ||
} | ||
return latestBlockHeight; | ||
} | ||
|
||
export function clear(): void { | ||
if (config.NODE_ENV !== NodeEnv.TEST) { | ||
throw new Error('clear cannot be used in non-test env'); | ||
} | ||
|
||
latestBlockHeight = ''; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.