-
Notifications
You must be signed in to change notification settings - Fork 2
feat: add missing data checker (works in conjunction with archiver mi… #87
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?
Changes from 1 commit
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,100 @@ | ||||||||||||||||||||||||||||||
| import * as Crypto from '../src/utils/crypto' | ||||||||||||||||||||||||||||||
| import * as Cycle from '../src/storage/cycle' | ||||||||||||||||||||||||||||||
| import * as Transaction from '../src/storage/transaction' | ||||||||||||||||||||||||||||||
| import * as Account from '../src/storage/account' | ||||||||||||||||||||||||||||||
| import * as Receipt from '../src/storage/receipt' | ||||||||||||||||||||||||||||||
| import * as Storage from '../src/storage' | ||||||||||||||||||||||||||||||
| import { config, overrideDefaultConfig } from '../src/config' | ||||||||||||||||||||||||||||||
| import { readFileSync, writeFileSync } from 'fs' | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| interface MissingData { | ||||||||||||||||||||||||||||||
| cycles: Array<{ counter: number; majorityHash?: string }> | ||||||||||||||||||||||||||||||
| receipts: Array<{ id: string; cycle: number; majorityHash?: string }> | ||||||||||||||||||||||||||||||
| accounts: Array<{ id: string; majorityHash?: string; cycle?: number; cycleNumber?: number }> | ||||||||||||||||||||||||||||||
| transactions: Array<{ id: string; cycle: number; majorityHash?: string; cycleNumber?: number }> | ||||||||||||||||||||||||||||||
| timestamp: number | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| const scriptConfig = { | ||||||||||||||||||||||||||||||
| files: { | ||||||||||||||||||||||||||||||
| inputFile: 'test.json', | ||||||||||||||||||||||||||||||
| outputFile: 'missing-collector-data.json', | ||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| async function main(): Promise<void> { | ||||||||||||||||||||||||||||||
| overrideDefaultConfig(process.env, process.argv) | ||||||||||||||||||||||||||||||
| Crypto.setCryptoHashKey(config.hashKey) | ||||||||||||||||||||||||||||||
| await Storage.initializeDB() | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| // read missingDataFile that's a json file | ||||||||||||||||||||||||||||||
| const missingDataFile = scriptConfig.files.inputFile | ||||||||||||||||||||||||||||||
| // eslint-disable-next-line security/detect-non-literal-fs-filename | ||||||||||||||||||||||||||||||
| const missingData = JSON.parse(readFileSync(missingDataFile, 'utf8')) as MissingData | ||||||||||||||||||||||||||||||
| if (!missingData) { | ||||||||||||||||||||||||||||||
| console.error('No missing data found in the file.') | ||||||||||||||||||||||||||||||
| return | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| const nonPatchedData: MissingData = { | ||||||||||||||||||||||||||||||
| cycles: [], | ||||||||||||||||||||||||||||||
| receipts: [], | ||||||||||||||||||||||||||||||
| accounts: [], | ||||||||||||||||||||||||||||||
| transactions: [], | ||||||||||||||||||||||||||||||
| timestamp: Date.now(), | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| // check cycles table | ||||||||||||||||||||||||||||||
| for (const cycle of missingData.cycles) { | ||||||||||||||||||||||||||||||
| const cycleRecord = await Cycle.queryCycleByCounter(cycle.counter) | ||||||||||||||||||||||||||||||
| if (!cycleRecord) { | ||||||||||||||||||||||||||||||
| console.log(`Missing cycle record for cycle ${cycle.counter}`) | ||||||||||||||||||||||||||||||
| nonPatchedData.cycles.push(cycle) | ||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||
| console.log(`Cycle record for cycle ${cycle.counter} exists`) | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| // check receipts table | ||||||||||||||||||||||||||||||
| for (const receipt of missingData.receipts) { | ||||||||||||||||||||||||||||||
| const receiptRecord = Receipt.queryReceiptByReceiptId(receipt.id) | ||||||||||||||||||||||||||||||
| if (!receiptRecord) { | ||||||||||||||||||||||||||||||
| console.log(`Missing receipt record for receipt ${receipt.id}`) | ||||||||||||||||||||||||||||||
| nonPatchedData.receipts.push(receipt) | ||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||
| console.log(`Receipt record for receipt ${receipt.id} exists`) | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| // check transactions table | ||||||||||||||||||||||||||||||
| for (const transaction of missingData.transactions) { | ||||||||||||||||||||||||||||||
| const transactionRecord = await Transaction.queryTransactionByTxId(transaction.id) | ||||||||||||||||||||||||||||||
| if (!transactionRecord) { | ||||||||||||||||||||||||||||||
| console.log(`Missing transaction record for transaction ${transaction.id}`) | ||||||||||||||||||||||||||||||
| nonPatchedData.transactions.push(transaction) | ||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||
| console.log(`Transaction record for transaction ${transaction.id} exists`) | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| // check accounts table | ||||||||||||||||||||||||||||||
| for (const account of missingData.accounts) { | ||||||||||||||||||||||||||||||
| const accountRecord = Account.queryAccountByAccountId(account.id) | ||||||||||||||||||||||||||||||
| if (!accountRecord) { | ||||||||||||||||||||||||||||||
| console.log(`Missing account record for account ${account.id}`) | ||||||||||||||||||||||||||||||
| nonPatchedData.accounts.push(account) | ||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||
| console.log(`Account record for account ${account.id} exists`) | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
Comment on lines
+79
to
+85
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. Suggestion: The call to
Suggested change
|
||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| // write nonPatchedData to missingDataFile | ||||||||||||||||||||||||||||||
| const outputFile = scriptConfig.files.outputFile | ||||||||||||||||||||||||||||||
| // eslint-disable-next-line security/detect-non-literal-fs-filename | ||||||||||||||||||||||||||||||
| writeFileSync(outputFile, JSON.stringify(nonPatchedData, null, 2), 'utf8') | ||||||||||||||||||||||||||||||
| console.log(`Missing data written to ${outputFile}`) | ||||||||||||||||||||||||||||||
| // close database | ||||||||||||||||||||||||||||||
| Storage.closeDatabase() | ||||||||||||||||||||||||||||||
| console.log('Database closed') | ||||||||||||||||||||||||||||||
| console.log('Script completed successfully') | ||||||||||||||||||||||||||||||
| process.exit(0) | ||||||||||||||||||||||||||||||
|
Comment on lines
+94
to
+97
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. Suggestion:
Suggested change
|
||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| main() | ||||||||||||||||||||||||||||||
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: The call to
Receipt.queryReceiptByReceiptIdis likely asynchronous, but it is not awaited. This can causereceiptRecordto be a Promise, leading to incorrect logic and missed missing receipts. [possible issue, importance: 8]