diff --git a/client/src/util/orchTester.js b/client/src/util/orchTester.js index cc68ce4..01cc84a 100644 --- a/client/src/util/orchTester.js +++ b/client/src/util/orchTester.js @@ -116,7 +116,7 @@ const batchPostStats = async function () { "Content-Length": postData.length, }, }; - console.log("Uploading stats of " + postData.length + "B"); + console.log("Uploading " + postData.length + " B of stats"); var req; if (CONF_MASTER_DOMAIN == "127.0.0.1" || CONF_MASTER_DOMAIN == "localhost") { req = http.request(options, (res) => { diff --git a/master/package.json b/master/package.json index 44d2319..0ded705 100644 --- a/master/package.json +++ b/master/package.json @@ -17,6 +17,7 @@ "dependencies": { "ethers": "^6.8.1", "express": "^4.17.1", + "node-persist": "^3.1.3", "npm": "^8.5.2", "prom-client": "^14.0.1" }, diff --git a/master/src/routes/hodler.js b/master/src/routes/hodler.js index 2b10b5e..e7ad0bf 100644 --- a/master/src/routes/hodler.js +++ b/master/src/routes/hodler.js @@ -1,6 +1,7 @@ const express = require("express"); const client = require("prom-client"); const { ethers } = require("ethers"); +const storage = require("node-persist"); const { CONF_API_L1_HTTP, CONF_API_L1_KEY, @@ -17,7 +18,9 @@ const { */ -const l1provider = new ethers.JsonRpcProvider(CONF_API_L1_HTTP + CONF_API_L1_KEY); +const l1provider = new ethers.JsonRpcProvider( + CONF_API_L1_HTTP + CONF_API_L1_KEY +); const masterRouter = express.Router(); const register = new client.Registry(); @@ -85,6 +88,7 @@ function sleep(ms) { let ensDomainCache = {}; let orchCache = {}; let lastLeaderboardCheck = 0; +let isSynced = false; /* @@ -105,7 +109,7 @@ const getEnsDomain = async function (addr) { let ensObj; if (!ensDomain) { let domain = null; - if (cached){ + if (cached) { domain = cached.domain; } ensObj = { @@ -129,6 +133,7 @@ const getEnsDomain = async function (addr) { ensObj.timestamp ); ensDomainCache[addr] = ensObj; + await storage.setItem("ensDomainCache", ensDomainCache); return ensObj.domain ? ensObj.domain : ensObj.address; } catch (err) { console.log(err); @@ -330,6 +335,7 @@ const onOrchUpdate = async function (id, obj, tag, region, livepeer_regions) { newObj.instances[obj.resolv.resolvedTarget] = newInstance; newObj.regionalStats[tag] = newRegion; orchCache[id.toLowerCase()] = newObj; + await storage.setItem("orchCache", orchCache); // Update prometheus stats updatePrometheus(tag, id, obj.resolv.resolvedTarget, newObj); @@ -357,6 +363,10 @@ Public endpoints // Mutate state with new stats masterRouter.post("/collectStats", async (req, res) => { + if (!isSynced) { + res.send("busy"); + return; + } try { const { batchResults, tag, key, region, livepeer_regions } = req.body; if (!batchResults || !tag || !key || !region || !livepeer_regions) { @@ -422,7 +432,10 @@ const updateScore = async function (address) { const newSR = instance.round_trip_time / instance.seg_duration; let latitude = null; let longitude = null; - for (const [resolvedTarget, instance] of orchCache[address.toLowerCase()].instances) { + console.log(address, orchCache[address.toLowerCase()]); + for (const [resolvedTarget, instance] of orchCache[ + address.toLowerCase() + ].instances) { if (instance.livepeer_regions[region]) { latitude = instance.livepeer_regions[region].latitude; longitude = instance.livepeer_regions[region].longitude; @@ -459,7 +472,8 @@ const updateScore = async function (address) { } } if (hasEdited) { - orchCache[address.toLowerCase()].leaderboardResults.lastTime = new Date().getTime(); + orchCache[address.toLowerCase()].leaderboardResults.lastTime = + new Date().getTime(); } }; @@ -469,6 +483,27 @@ const updateOrchScores = async function () { } }; +const recoverStorage = async function () { + await storage.init({ + stringify: JSON.stringify, + parse: JSON.parse, + encoding: "utf8", + logging: false, + ttl: false, + forgiveParseErrors: false, + }); + storedDomains = await storage.getItem("ensDomainCache"); + if (storedDomains) { + ensDomainCache = storedDomains; + } + storedOrchs = await storage.getItem("orchCache"); + if (storedOrchs) { + orchCache = storedOrchs; + } + isSynced = true; +}; +recoverStorage(); + const runTests = async function () { try { const now = new Date().getTime();