-
Couldn't load subscription status.
- Fork 15
Improvements for cycle tracker #266
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
base: dev
Are you sure you want to change the base?
Conversation
PR Reviewer Guide 🔍Here are some key observations to aid the review process:
|
| await callback(cycle) | ||
| // Update the checkpoint status to mark it as unified after syncing | ||
| await updateCheckpointStatusField(cycle, CheckpointStatusType.CYCLE, true) | ||
| await updateCheckpointStatusField(cycle, CheckpointStatusType.RECEIPT, true) | ||
| await updateCheckpointStatusField(cycle, CheckpointStatusType.ORIGINAL_TX, true) | ||
| Logger.mainLogger.debug(`[processCyclesNeedingSync] Updated cycle ${cycle} unifiedStatus to true`) | ||
| } |
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.
Suggestion: Updating all checkpoint status fields to true immediately after the callback may cause data inconsistency if the callback fails or is only partially successful. Ensure that the callback completes successfully before marking statuses as unified, and consider handling errors from the callback to avoid marking failed cycles as complete. [possible issue, importance: 8]
| await callback(cycle) | |
| // Update the checkpoint status to mark it as unified after syncing | |
| await updateCheckpointStatusField(cycle, CheckpointStatusType.CYCLE, true) | |
| await updateCheckpointStatusField(cycle, CheckpointStatusType.RECEIPT, true) | |
| await updateCheckpointStatusField(cycle, CheckpointStatusType.ORIGINAL_TX, true) | |
| Logger.mainLogger.debug(`[processCyclesNeedingSync] Updated cycle ${cycle} unifiedStatus to true`) | |
| } | |
| try { | |
| await callback(cycle) | |
| // Update the checkpoint status to mark it as unified after successful sync | |
| await updateCheckpointStatusField(cycle, CheckpointStatusType.CYCLE, true) | |
| await updateCheckpointStatusField(cycle, CheckpointStatusType.RECEIPT, true) | |
| await updateCheckpointStatusField(cycle, CheckpointStatusType.ORIGINAL_TX, true) | |
| Logger.mainLogger.debug(`[processCyclesNeedingSync] Updated cycle ${cycle} unifiedStatus to true`) | |
| } catch (err) { | |
| Logger.mainLogger.error(`[processCyclesNeedingSync] Failed to sync cycle ${cycle}: ${err}`) | |
| // Do not update checkpoint status if callback fails | |
| } |
| export async function updateLastUpdatedCycle(cycle: number): Promise<void> { | ||
| const lastUpdatedCycle = await getLastUpdatedCycle() | ||
| if (cycle > lastUpdatedCycle) { | ||
| try { | ||
| const trackerData: CycleTrackerData = { |
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.
Suggestion: If cycle is less than or equal to lastUpdatedCycle, the function silently does nothing. This could mask logic errors or missed updates. Log a warning or throw an error when an attempt is made to update with a non-incremental cycle value to aid debugging and data integrity. [general, importance: 6]
New proposed code:
export async function updateLastUpdatedCycle(cycle: number): Promise<void> {
const lastUpdatedCycle = await getLastUpdatedCycle()
if (cycle > lastUpdatedCycle) {
try {
const trackerData: CycleTrackerData = {
lastUpdatedCycle: cycle,
+ ...
+ } else {
+ Logger.mainLogger.warn(`Attempted to update lastUpdatedCycle to ${cycle}, which is not greater than current value ${lastUpdatedCycle}`)
+ }| await updateCheckpointStatusField(cycle, CheckpointStatusType.CYCLE, true) | ||
| await updateCheckpointStatusField(cycle, CheckpointStatusType.RECEIPT, true) | ||
| await updateCheckpointStatusField(cycle, CheckpointStatusType.ORIGINAL_TX, true) | ||
| Logger.mainLogger.debug(`[processCyclesNeedingSync] Updated cycle ${cycle} unifiedStatus to true`) |
Check warning
Code scanning / CodeQL
Log injection Medium
user-provided value
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix
AI 5 months ago
To fix the issue, we need to sanitize the cycle value before logging it. Since cycle is expected to be a numeric value, we can ensure it is properly sanitized by converting it to a string and removing any unexpected characters (e.g., newline or carriage return characters). This can be achieved using String.prototype.replace or similar methods.
The fix will involve modifying the log statement on line 514 in src/dbstore/checkpointStatus.ts to sanitize the cycle value before including it in the log message.
-
Copy modified lines R514-R515
| @@ -513,3 +513,4 @@ | ||
| await updateCheckpointStatusField(cycle, CheckpointStatusType.ORIGINAL_TX, true) | ||
| Logger.mainLogger.debug(`[processCyclesNeedingSync] Updated cycle ${cycle} unifiedStatus to true`) | ||
| const sanitizedCycle = String(cycle).replace(/\n|\r/g, ""); | ||
| Logger.mainLogger.debug(`[processCyclesNeedingSync] Updated cycle ${sanitizedCycle} unifiedStatus to true`) | ||
| } |
PR Type
Enhancement, Bug fix
Description
Refactored cycle tracker to use async/await for file and DB operations
Improved fallback logic for missing cycle tracker file
Added DB query for specific unified cycle skipping recent cycles
Updated checkpoint sync logic to mark cycles unified after syncing
Changes walkthrough 📝
cycleTracker.ts
Async refactor and fallback improvements for cycle trackersrc/utils/cycleTracker.ts
getLastUpdatedCycleandupdateLastUpdatedCycletoasync/await
checkpointStatus.ts
Add DB unified cycle query and sync status updatesrc/dbstore/checkpointStatus.ts
getSpecificUnifiedCyclefor DB-level cycle skippingprocessCyclesNeedingSyncto mark cycles unified after syncingData.ts
Use async cycle tracker in data sync functionssrc/Data/Data.ts
getLastUpdatedCycleto async/awaitCheckpointV2.ts
Use async cycle tracker in checkpoint syncsrc/checkpoint/CheckpointV2.ts
getLastUpdatedCycleserver.ts
Use async cycle tracker in server startup and syncsrc/server.ts
getLastUpdatedCyclein server startup and sync