Skip to content

Commit 616133d

Browse files
fix: repair chunk file
1 parent 28ade29 commit 616133d

1 file changed

Lines changed: 44 additions & 14 deletions

File tree

scripts/hyp-repair.ts

Lines changed: 44 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -509,43 +509,73 @@ async function repairChain(chain: string, file: string, args: any) {
509509
await fillMissingBlocksFromFile(args.host, chain, file, args.dry);
510510
}
511511

512-
async function fillMissingBlocksFromFile(host: string, chain: string, file: string, dryRun: string) {
512+
async function fillMissingBlocksFromFile(host, chain, file, dryRun) {
513513
const config = readConnectionConfig();
514514
const controlPort = config.chains[chain].control_port;
515515
let hyperionIndexer = `ws://localhost:${controlPort}`;
516516
if (host) {
517-
hyperionIndexer = 'ws://' + host + ':' + controlPort;
517+
hyperionIndexer = `ws://${host}:${controlPort}`;
518518
}
519519
const controller = new WebSocket(hyperionIndexer + '/local');
520-
controller.on('open', async () => {
521-
console.log('Connected to Hyperion Controller');
522-
const parsedFile = JSON.parse(readFileSync(file).toString());
523-
if (!dryRun) {
520+
521+
// Function to send Chunk
522+
async function sendChunk(chunk): Promise<void> {
523+
return new Promise((resolve, reject) => {
524524
const payload = {
525525
"event": "fill_missing_blocks",
526-
"data": parsedFile
526+
"data": chunk
527527
};
528528
controller.send(JSON.stringify(payload));
529+
530+
// Wait repair_completed confirmation
531+
controller.once('message', (data) => {
532+
const parsed = JSON.parse(data.toString());
533+
if (parsed.event === 'repair_completed') {
534+
// console.log(`Hyperion repair completed for chunk!`);
535+
resolve();
536+
}
537+
});
538+
});
539+
}
540+
541+
controller.on('open', async () => {
542+
console.log('Connected to Hyperion Controller');
543+
const parsedFile = JSON.parse(readFileSync(file).toString());
544+
const chunkSize = 20; // Chunk size
545+
546+
if (!dryRun) {
547+
const totalChunks = Math.ceil(parsedFile.length / chunkSize);
548+
let completedChunks = 0;
549+
for (let i = 0; i < parsedFile.length; i += chunkSize) {
550+
const progress = (completedChunks / totalChunks) * 100;
551+
const progressBar = Array(Math.round(progress / 2)).fill('#').join('');
552+
process.stdout.write(`Progress: [${progressBar}] ${progress.toFixed(2)}% \r`);
553+
// console.log(`Progress: [${progressBar}] ${progress.toFixed(2)}%`);
554+
555+
const chunk = parsedFile.slice(i, i + chunkSize);
556+
await sendChunk(chunk);
557+
558+
// Update progess bar
559+
completedChunks++;
560+
}
561+
console.log();
562+
controller.close();
529563
} else {
530564
console.log('Dry run, skipping repair');
531565
controller.close();
532566
}
533567
});
534-
controller.on('message', (data) => {
535-
const parsed = JSON.parse(data.toString());
536-
if (parsed.event === 'repair_completed') {
537-
console.log(`Hyperion repair completed!`);
538-
controller.close();
539-
}
540-
});
568+
541569
controller.on('close', () => {
542570
console.log('Disconnected from Hyperion Controller');
543571
});
572+
544573
controller.on('error', (err) => {
545574
console.log(err);
546575
});
547576
}
548577

578+
549579
function viewFile(file: string) {
550580
const data = readFileSync(file, 'utf8');
551581
const parsed = JSON.parse(data);

0 commit comments

Comments
 (0)