From 96f3985c85ad00b25a622f3e727614fc0029a9b9 Mon Sep 17 00:00:00 2001 From: Rohit Verma Date: Sun, 10 Mar 2019 23:29:10 -0300 Subject: [PATCH] adding hourly script, modifying script a bit --- scripts/hourlySaveToJson.js | 58 +++++++++++++++++++++++++++++ scripts/mergeAndSaveJsonData.js | 18 +++++++++ scripts/saveBlockchainDataToJson.js | 13 ------- scripts/saveCycleDataToJson.js | 4 ++ 4 files changed, 80 insertions(+), 13 deletions(-) create mode 100644 scripts/hourlySaveToJson.js create mode 100644 scripts/mergeAndSaveJsonData.js diff --git a/scripts/hourlySaveToJson.js b/scripts/hourlySaveToJson.js new file mode 100644 index 0000000..3fd34fd --- /dev/null +++ b/scripts/hourlySaveToJson.js @@ -0,0 +1,58 @@ +import TezosRpc from '../src/services/rpc/rpc' +import Helper from '../src/services/utils/helper' +const fs = require('fs'); +const { promisify } = require("util"); +const writeFile = promisify(fs.writeFile); +const { fork } = require('child_process'); + +(async () => { + const tezosConfig = require('../static/config.json') + const tezosRpc = new TezosRpc(tezosConfig.tezosRpcAddress) + const block = 'head' + const mostRecentCompletedCycle = await tezosRpc.getHeadCycle() + let cycle = mostRecentCompletedCycle + 6 + await tezosRpc.setCycle(cycle) + const rawSnapshotData = require('../static/snapshotData.json') + const cycleDataFromJson = require('../static/allCyclesData.json') + + // save rights data + snapshot data for latest future cycle + const latestCycleFromSnapshotJson = rawSnapshotData[rawSnapshotData.length-1]['cycleNumber'] + if(latestCycleFromSnapshotJson != cycle) { + const snapshotBlockNumber = await tezosRpc.getSnapshotBlockForCycle('head') + const snapshotNumber = tezosRpc.snapshotNumber + rawSnapshotData.push({ + 'cycleNumber': cycle, + 'snapshotBlockNumber': snapshotBlockNumber, + 'snapshotNumber': tezosRpc.snapshotNumber + }) + await writeFile('../static/snapshotData.json', JSON.stringify(rawSnapshotData)); + console.log('cycle: ', cycle, ', snapshotNumber: ', snapshotNumber, ', snapshotBlockNumber: ', snapshotBlockNumber) + console.log('Saved: snapshotData.json'); + + const process = fork('./saveCycleDataToJson.js'); + let snapshotData = {} + snapshotData[cycle] = snapshotBlockNumber + process.send({mostRecentCompletedCycle, block, snapshotData, cycle}) + process.on('message', async (data) => { + cycleDataFromJson[cycle] = data[cycle] + await writeFile('../static/allCyclesData.json', JSON.stringify(cycleDataFromJson)) + console.log('Saved: allCyclesData.json with latest future cycle'); + }) + } + + // save rewards data for latest baked cycle + const mostRecentCompletedCycleDatFromJson = cycleDataFromJson[mostRecentCompletedCycle] + if (mostRecentCompletedCycleDatFromJson['prediction']) { + cycle = mostRecentCompletedCycle + await tezosRpc.setCycle(mostRecentCompletedCycle) + const process = fork('./saveCycleDataToJson.js'); + const snapshotData = new Helper().parseSnapshotData(rawSnapshotData)['snapshotblockNumberData'] + process.send({mostRecentCompletedCycle, block, snapshotData, cycle}) + process.on('message', async (data) => { + cycleDataFromJson[cycle] = data[cycle] + await writeFile('../static/allCyclesData.json', JSON.stringify(cycleDataFromJson)) + console.log('Saved: allCyclesData.json with latest baked cycle'); + }) + } + +})() diff --git a/scripts/mergeAndSaveJsonData.js b/scripts/mergeAndSaveJsonData.js new file mode 100644 index 0000000..313a9da --- /dev/null +++ b/scripts/mergeAndSaveJsonData.js @@ -0,0 +1,18 @@ +import TezosRpc from '../src/services/rpc/rpc' +const fs = require('fs'); +const util = require('util'); +const readFile = util.promisify(fs.readFile); +const writeFile = util.promisify(fs.writeFile); + +(async () => { + const tezosConfig = require('../static/config.json') + const tezosRpc = new TezosRpc(tezosConfig.tezosRpcAddress) + const mostRecentCompletedCycle = await tezosRpc.getHeadCycle() + let allCyclesData = {} + for(let i = 7; i <= mostRecentCompletedCycle + 6; i++){ + const cycleData = await readFile(i+'.json') + allCyclesData = { ...allCyclesData, ...JSON.parse(cycleData.toString('utf8'))} + } + await writeFile('../static/allCyclesData.json', JSON.stringify(allCyclesData)) + console.log('Saved: allCyclesData.json') +})() diff --git a/scripts/saveBlockchainDataToJson.js b/scripts/saveBlockchainDataToJson.js index 1a6ee73..bf29657 100644 --- a/scripts/saveBlockchainDataToJson.js +++ b/scripts/saveBlockchainDataToJson.js @@ -26,17 +26,4 @@ const util = require('util'); }); }) } - - const readFile = util.promisify(fs.readFile); - let allCyclesData = {} - for(let i = 7; i <= mostRecentCompletedCycle + 6; i++){ - const cycleData = await readFile('../static/'+i+'.json') - allCyclesData = { ...allCyclesData, ...JSON.parse(cycleData.toString('utf8'))} - } - fs.writeFile('../static/allCyclesData.json', JSON.stringify(allCyclesData), function(err) { - if(err) { - return console.log(err); - } - console.log('Saved: ', "allCyclesData.json"); - }); })() diff --git a/scripts/saveCycleDataToJson.js b/scripts/saveCycleDataToJson.js index 0737642..ded091e 100644 --- a/scripts/saveCycleDataToJson.js +++ b/scripts/saveCycleDataToJson.js @@ -7,6 +7,7 @@ process.on('message', async ({mostRecentCompletedCycle, block, snapshotData, cyc tezosRpc.setSnapshotBlockNumber(snapshotData[tezosRpc.cycle]) let endorsingDataForCycle = {} let bakingDataForCycle = {} + let prediction = false if (cycle <= mostRecentCompletedCycle) { // get data from metadata - already baked @@ -17,6 +18,7 @@ process.on('message', async ({mostRecentCompletedCycle, block, snapshotData, cyc // get rights for future cycles endorsingDataForCycle = await tezosRpc.getEndorsingRightsOfCycle(block, cycle) bakingDataForCycle = await tezosRpc.getBakingRightsOfCycle(block, cycle) + prediction = true } const allDelegatesArray = Array.from(new Set(Object.keys(endorsingDataForCycle).concat(Object.keys(bakingDataForCycle)))) @@ -30,6 +32,7 @@ process.on('message', async ({mostRecentCompletedCycle, block, snapshotData, cyc const bakingRewards = bakingDataForCycle[delegate] || 0 tezosRpc.delegateHash = delegate const cycleData = await tezosRpc.getCycleData() + cycleRewardsData[cycle]['prediction'] = prediction cycleRewardsData[cycle][delegate] = { 'endorsingRewards': endorsingRewards, 'bakingRewards': bakingRewards, @@ -37,4 +40,5 @@ process.on('message', async ({mostRecentCompletedCycle, block, snapshotData, cyc } } process.send(cycleRewardsData) + process.exit() });