Skip to content

Commit

Permalink
Extract utils/epoch-manager
Browse files Browse the repository at this point in the history
  • Loading branch information
vgrichina committed Oct 21, 2024
1 parent e216821 commit 9fba77e
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 39 deletions.
40 changes: 1 addition & 39 deletions scripts/fetch-epochs.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,45 +23,7 @@ async function getEpochValidators(blockHeight) {
return data.result;
}

const fs = require('fs').promises;
const path = require('path');
const zlib = require('zlib');
const util = require('util');

const gzip = util.promisify(zlib.gzip);
const gunzip = util.promisify(zlib.gunzip);

const epochDataDir = './epoch-data';
const indexFilePath = path.join(epochDataDir, 'index.json.gz');

async function readEpochSummaries() {
try {
const compressedIndexData = await fs.readFile(indexFilePath);
const indexData = await gunzip(compressedIndexData);
const summaries = JSON.parse(indexData.toString());
return summaries.sort((a, b) => a.epochHeight - b.epochHeight);
} catch (error) {
if (error.code !== 'ENOENT') {
console.error('Error reading index.json.gz:', error);
}

return [];
}
}

async function writeEpochSummaries(summaries) {
const compressedSummaries = await gzip(JSON.stringify(summaries));
await fs.writeFile(indexFilePath, compressedSummaries);
}

async function writeEpochData(epochHeight, data) {
await fs.mkdir(epochDataDir, { recursive: true });
const compressedData = await gzip(JSON.stringify(data));
await fs.writeFile(
path.join(epochDataDir, `${epochHeight}.json.gz`),
compressedData
);
}
const { readEpochSummaries, writeEpochSummaries, writeEpochData } = require('../utils/epoch-manager');

const EPOCH_CHECK_INTERVAL_MS = 15 * 1000;

Expand Down
45 changes: 45 additions & 0 deletions utils/epoch-manager.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
const fs = require('fs').promises;
const path = require('path');
const zlib = require('zlib');
const util = require('util');

const gzip = util.promisify(zlib.gzip);
const gunzip = util.promisify(zlib.gunzip);

const epochDataDir = './epoch-data';
const indexFilePath = path.join(epochDataDir, 'index.json.gz');

async function readEpochSummaries() {
try {
const compressedIndexData = await fs.readFile(indexFilePath);
const indexData = await gunzip(compressedIndexData);
const summaries = JSON.parse(indexData.toString());
return summaries.sort((a, b) => a.epochHeight - b.epochHeight);
} catch (error) {
if (error.code !== 'ENOENT') {
console.error('Error reading index.json.gz:', error);
}

return [];
}
}

async function writeEpochSummaries(summaries) {
const compressedSummaries = await gzip(JSON.stringify(summaries));
await fs.writeFile(indexFilePath, compressedSummaries);
}

async function writeEpochData(epochHeight, data) {
await fs.mkdir(epochDataDir, { recursive: true });
const compressedData = await gzip(JSON.stringify(data));
await fs.writeFile(
path.join(epochDataDir, `${epochHeight}.json.gz`),
compressedData
);
}

module.exports = {
readEpochSummaries,
writeEpochSummaries,
writeEpochData
};

0 comments on commit 9fba77e

Please sign in to comment.