From 4416ab5ad351f20db619ed72b395386b69ef3726 Mon Sep 17 00:00:00 2001 From: sairajzero Date: Fri, 18 Feb 2022 19:24:09 +0530 Subject: [PATCH] Adding Blockchain refresh interval - Added blockchain refresh interval - Updating some constants - Disabled various console.debug --- src/_constants.js | 9 +++++---- src/app.js | 4 ++-- src/backup/head.js | 8 ++++---- src/backup/slave.js | 6 +++--- src/coupling.js | 4 ++-- src/main.js | 9 +++++++-- src/market.js | 4 ++-- src/price.js | 2 +- src/request.js | 2 +- 9 files changed, 27 insertions(+), 21 deletions(-) diff --git a/src/_constants.js b/src/_constants.js index d329b3c..29cdad8 100644 --- a/src/_constants.js +++ b/src/_constants.js @@ -1,6 +1,7 @@ module.exports = { app: { - REFRESH_INTERVAL: 1 * 60 * 1000 // 1 min + BLOCKCHAIN_REFRESH_INTERVAL: 1 * 60 * 60 * 1000, // 1 hr + PERIOD_INTERVAL: 15 * 60 * 1000 // 15 min }, request: { MAX_SESSION_TIMEOUT: 60 * 24 * 60 * 60 * 1000, //60 days @@ -16,14 +17,14 @@ module.exports = { MAX_DOWN_PER_DAY: 4.8 / 100, //max 4.8% dec MAX_UP_PER_DAY: 12 / 100, //max 12% inc TOP_RANGE: 10 / 100, //top 10% - REC_HISTORY_INTERVAL: 30 * 60 * 1000 //30 mins + REC_HISTORY_INTERVAL: 1 * 60 * 60 * 1000, // 1 hr }, backup: { SHARE_THRESHOLD: 50 / 100, // 50% HASH_N_ROW: 100, SINK_KEY_INDICATOR: '$$$', - BACKUP_INTERVAL: 1 * 60 * 1000, //1 min + BACKUP_INTERVAL: 5 * 60 * 1000, //5 min BACKUP_SYNC_TIMEOUT: 10 * 60 * 1000, //10 mins - CHECKSUM_INTERVAL: 15, //times of BACKUP_INTERVAL + CHECKSUM_INTERVAL: 100, //times of BACKUP_INTERVAL } } \ No newline at end of file diff --git a/src/app.js b/src/app.js index 8adad10..3f2fa66 100644 --- a/src/app.js +++ b/src/app.js @@ -5,7 +5,7 @@ const express = require('express'); const Request = require('./request'); const { - REFRESH_INTERVAL + PERIOD_INTERVAL } = require("./_constants")["app"]; module.exports = function App(secret, DB) { @@ -141,7 +141,7 @@ module.exports = function App(secret, DB) { Request.resume(); Request.periodicProcess(); if (periodInstance === null) - periodInstance = setInterval(Request.periodicProcess, REFRESH_INTERVAL); + periodInstance = setInterval(Request.periodicProcess, PERIOD_INTERVAL); } Object.defineProperty(self, "periodInstance", { diff --git a/src/backup/head.js b/src/backup/head.js index 433d6bf..5232ebe 100644 --- a/src/backup/head.js +++ b/src/backup/head.js @@ -107,13 +107,13 @@ collectShares.retrive = function(floID, sinkID, share) { return console.error("Something is wrong! Slaves are sending different sinkID"); if (share.startsWith(SINK_KEY_INDICATOR)) { let sinkKey = share.substring(SINK_KEY_INDICATOR.length); - console.debug("Received sinkKey:", sinkID, sinkKey); + //console.debug("Received sinkKey:", sinkID, sinkKey); self.verify(sinkKey); } else self.shares[floID] = share.split("|"); try { let sinkKey = floCrypto.retrieveShamirSecret([].concat(...Object.values(self.shares))); - console.debug("Retrived sinkKey:", sinkID, sinkKey); + //console.debug("Retrived sinkKey:", sinkID, sinkKey); self.verify(sinkKey); } catch { //Unable to retrive sink private key. Waiting for more shares! Do nothing for now @@ -212,7 +212,7 @@ function informLiveNodes(init) { console.warn("sinkID and sinkKey in DB are not pair!"); storeSink(global.sinkID, global.sinkPrivKey); } - console.debug("Loaded sinkKey:", global.sinkID, global.sinkPrivKey) + //console.debug("Loaded sinkKey:", global.sinkID, global.sinkPrivKey) sendSharesToNodes(global.sinkID, generateShares(global.sinkPrivKey)) } else { //Share is present in DB, try to collect remaining shares and retrive sinkKey @@ -280,7 +280,7 @@ function startBackupTransmitter(server) { try { let invalid = null, request = JSON.parse(message); - console.debug(request); + //console.debug(request); if (!nodeList.includes(request.floID)) invalid = `floID ${request.floID} not in nodeList`; else if (request.floID !== floCrypto.getFloID(request.pubKey)) diff --git a/src/backup/slave.js b/src/backup/slave.js index 0a1e253..d1880e0 100644 --- a/src/backup/slave.js +++ b/src/backup/slave.js @@ -146,7 +146,7 @@ function processDataFromMaster(message) { function storeSinkShare(sinkID, keyShare) { let encryptedShare = Crypto.AES.encrypt(floCrypto.decryptData(keyShare, global.myPrivKey), global.myPrivKey); - console.debug(Date.now(), '|sinkID:', sinkID, '|EnShare:', encryptedShare); + console.log(Date.now(), '|sinkID:', sinkID, '|EnShare:', encryptedShare); DB.query("INSERT INTO sinkShares (floID, share) VALUE (?, ?) AS new ON DUPLICATE KEY UPDATE share=new.share", [sinkID, encryptedShare]) .then(_ => null).catch(error => console.error(error)); } @@ -333,7 +333,7 @@ function verifyChecksum(checksum_ref) { for (let table in checksum) if (checksum[table] != checksum_ref[table]) mismatch.push(table); - console.debug("Checksum-mismatch:", mismatch); + //console.debug("Checksum-mismatch:", mismatch); if (!mismatch.length) //Checksum of every table is verified. resolve(true); else { //If one or more tables checksum is not correct, re-request the table data @@ -398,7 +398,7 @@ function verifyHash(hashes) { .then(_ => null); } else console.error(result[t].reason); - console.debug("Hash-mismatch", mismatch); + //console.debug("Hash-mismatch", mismatch); resolve(mismatch); }).catch(error => reject(error)) }) diff --git a/src/coupling.js b/src/coupling.js index 67c2f53..d09f4d9 100644 --- a/src/coupling.js +++ b/src/coupling.js @@ -17,8 +17,8 @@ function processCoupling(bestPairQueue) { bestPairQueue.get().then(pair_result => { let buyer_best = pair_result.buyOrder, seller_best = pair_result.sellOrder; - console.debug("Sell:", seller_best); - console.debug("Buy:", buyer_best); + //console.debug("Sell:", seller_best); + //console.debug("Buy:", buyer_best); spendAsset(bestPairQueue.asset, buyer_best, seller_best, pair_result.null_base).then(spent => { if (!spent.quantity) { //Happens when there are only Null-base assets diff --git a/src/main.js b/src/main.js index fbd3758..ce8caa7 100644 --- a/src/main.js +++ b/src/main.js @@ -11,6 +11,10 @@ const App = require('./app'); const backup = require('./backup/head'); +const { + BLOCKCHAIN_REFRESH_INTERVAL +} = require("./_constants")["app"]; + var DB, app; function refreshData(startup = false) { @@ -81,7 +85,7 @@ function refreshDataFromBlockchain() { promises.push(DB.query("INSERT INTO LastTx (floID, num) VALUE (?, ?) AS new ON DUPLICATE KEY UPDATE num=new.num", [floGlobals.adminID, result.totalTxs])); //Check if all save process were successful Promise.allSettled(promises).then(results => { - console.debug(results.filter(r => r.status === "rejected")); + //console.debug(results.filter(r => r.status === "rejected")); if (results.reduce((a, r) => r.status === "rejected" ? ++a : a, 0)) console.warn("Some data might not have been saved in database correctly"); }); @@ -176,7 +180,7 @@ module.exports = function startServer(public_dir) { } global.PUBLIC_DIR = public_dir; - console.debug(PUBLIC_DIR, global.myFloID); + console.log("Logged in as", global.myFloID); Database(config["sql_user"], config["sql_pwd"], config["sql_db"], config["sql_host"]).then(db => { setDB(db); @@ -185,6 +189,7 @@ module.exports = function startServer(public_dir) { app.start(config['port']).then(result => { console.log(result); backup.init(app); + setInterval(refreshData, BLOCKCHAIN_REFRESH_INTERVAL) }).catch(error => console.error(error)) }).catch(error => console.error(error)) }).catch(error => console.error(error)); diff --git a/src/market.js b/src/market.js index ec10256..838a898 100644 --- a/src/market.js +++ b/src/market.js @@ -254,7 +254,7 @@ function withdrawFLO(floID, amount) { .then(_ => null).catch(error => console.error(error)) .finally(_ => resolve("Withdrawal was successful")); }).catch(error => { - console.debug(error); + console.error(error); DB.query("INSERT INTO OutputFLO (floID, amount, status) VALUES (?, ?, ?)", [floID, amount, "PENDING"]) .then(_ => null).catch(error => console.error(error)) .finally(_ => resolve("Withdrawal request is in process")); @@ -393,7 +393,7 @@ function withdrawToken(floID, token, amount) { .then(_ => null).catch(error => console.error(error)) .finally(_ => resolve("Withdrawal was successful")); }).catch(error => { - console.debug(error); + console.error(error); DB.query("INSERT INTO OutputToken (floID, token, amount, status) VALUES (?, ?, ?, ?)", [floID, token, amount, "PENDING"]) .then(_ => null).catch(error => console.error(error)) .finally(_ => resolve("Withdrawal request is in process")); diff --git a/src/price.js b/src/price.js index b3accf4..c2e439d 100644 --- a/src/price.js +++ b/src/price.js @@ -97,7 +97,7 @@ fetchRates.USD_INR = function() { function getRates(asset) { return new Promise((resolve, reject) => { loadRate(asset).then(_ => { - console.debug(asset, currentRate[asset]); + //console.debug(asset, currentRate[asset]); let cur_time = Date.now(); if (cur_time - lastTime[asset] < MIN_TIME) //Minimum time to update not crossed: No update required resolve(currentRate[asset]); diff --git a/src/request.js b/src/request.js index b64e7b6..d531e5a 100644 --- a/src/request.js +++ b/src/request.js @@ -57,7 +57,7 @@ function validateRequest(request, sign, pubKey) { } function storeRequest(floID, req_str, sign) { - console.debug(floID, req_str); + //console.debug(floID, req_str); DB.query("INSERT INTO RequestLog (floID, request, sign) VALUES (?,?,?)", [floID, req_str, sign]) .then(_ => null).catch(error => console.error(error)); }