-
Notifications
You must be signed in to change notification settings - Fork 129
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
[CT-839] Add blockHeight to subaccount websocket message #1585
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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; | ||
}); | ||
}); | ||
}); |
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 = ''; | ||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove unnecessary type annotation for - let latestBlockHeight: string = '';
+ let latestBlockHeight = ''; Committable suggestion
Suggested change
|
||||||||||||||||
|
||||||||||||||||
/** | ||||||||||||||||
* 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) { } | ||||||||||||||||
chenyaoy marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
/** | ||||||||||||||||
* 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; | ||||||||||||||||
Comment on lines
+43
to
+52
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Tip Codebase Verification The extracted context from the handler files shows that
Ensure that calls to Analysis chainEnsure robust error handling in the Scripts ExecutedThe following scripts were executed for the analysis: Script: #!/bin/bash
# Description: Verify robust error handling in the `getLatestBlockHeight` function.
# Test: Search for the function usage. Expect: Robust error handling.
rg --type typescript $'getLatestBlockHeight'
Length of output: 78 Script: #!/bin/bash
# Description: Verify robust error handling in the `getLatestBlockHeight` function.
# Test: Search for the function usage. Expect: Robust error handling.
rg --type ts 'getLatestBlockHeight'
Length of output: 3090 Script: #!/bin/bash
# Description: Extract and inspect the usage of `getLatestBlockHeight` in handler files to verify robust error handling.
# Extract lines around the usage of `getLatestBlockHeight` in handler files.
rg --type ts --context 5 'getLatestBlockHeight' indexer/services/vulcan/src/handlers/order-remove-handler.ts indexer/services/vulcan/src/handlers/order-place-handler.ts
Length of output: 4264 |
||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
export function clear(): void { | ||||||||||||||||
if (config.NODE_ENV !== NodeEnv.TEST) { | ||||||||||||||||
throw new Error('clear cannot be used in non-test env'); | ||||||||||||||||
} | ||||||||||||||||
Comment on lines
+56
to
+58
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Throwing errors in non-test environments could be risky. Consider using a safer approach to handle this condition. - throw new Error('clear cannot be used in non-test env');
+ logger.error('clear cannot be used in non-test env');
+ return; Committable suggestion
Suggested change
|
||||||||||||||||
|
||||||||||||||||
latestBlockHeight = ''; | ||||||||||||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider using type-only imports for
BlockTable
andBlockFromDatabase
as they are only used as types.