diff --git a/src/database.js b/src/database.js index 99d4343..b33000b 100644 --- a/src/database.js +++ b/src/database.js @@ -21,7 +21,7 @@ const Base_Tables = { Applications: { APP_NAME: "VARCHAR(64) NOT NULL", ADMIN_ID: "CHAR(34) NOT NULL", - SUB_ADMINS: "VARCHAR(MAX)", + SUB_ADMINS: "VARCHAR(3500)", PRIMARY: "KEY (APP_NAME)" } }; @@ -67,7 +67,7 @@ function Database(user, password, dbname, host = 'localhost') { for (let t in Base_Tables) statements.push("CREATE TABLE IF NOT EXISTS " + t + "( " + Object.keys(Base_Tables[t]).map(a => a + " " + Base_Tables[t][a]).join(", ") + " )"); - Promise.all(statements.forEach(s => db.query(s))) + Promise.all(statements.map(s => db.query(s))) .then(result => resolve(result)) .catch(error => reject(error)); }); @@ -147,17 +147,17 @@ function Database(user, password, dbname, host = 'localhost') { db.getBase = function() { return new Promise((resolve, reject) => { let tables = Object.keys(Base_Tables); - Promise.all(tables.forEach(t => db.query("SELECT * FROM " + t))).then(result => { + Promise.all(tables.map(t => db.query("SELECT * FROM " + t))).then(result => { let tmp = Object.fromEntries(tables.map((t, i) => [t, result[i]])); result = {}; result.lastTx = Object.fromEntries(tmp.LastTxs.map(a => [a.ID, a.N])); result.sn_config = Object.fromEntries(tmp.Configs.map(a => [a.NAME, a.VAL])); result.appList = Object.fromEntries(tmp.Applications.map(a => [a.APP_NAME, a.ADMIN_ID])); result.appSubAdmins = Object.fromEntries(tmp.Applications.map(a => [a.APP_NAME, a.SUB_ADMINS.split(",")])); - result.supernodes = Object.fromEntries(tmp.SuperNodes.map(a.FLO_ID, { + result.supernodes = Object.fromEntries(tmp.SuperNodes.map(a => [a.FLO_ID, { pubKey: a.PUB_KEY, uri: a.URI - })); + }])); resolve(result); }).catch(error => reject(error)); }); @@ -166,19 +166,19 @@ function Database(user, password, dbname, host = 'localhost') { db.createTable = function(snID) { return new Promise((resolve, reject) => { let statement = "CREATE TABLE IF NOT EXISTS _" + snID + " ( " + - H_struct.VECTOR_CLOCK + " VARCHAR(50) NOT NULL, " + + H_struct.VECTOR_CLOCK + " VARCHAR(52) NOT NULL, " + H_struct.SENDER_ID + " CHAR(34) NOT NULL, " + H_struct.RECEIVER_ID + " CHAR(34) NOT NULL, " + - H_struct.APPLICATION + " VARCHAR(128) NOT NULL, " + - H_struct.TYPE + " VARCHAR(1024), " + + H_struct.APPLICATION + " TINYTEXT NOT NULL, " + + H_struct.TYPE + " TINYTEXT, " + B_struct.MESSAGE + " TEXT NOT NULL, " + B_struct.TIME + " INT NOT NULL, " + B_struct.SIGNATURE + " VARCHAR(160) NOT NULL, " + B_struct.PUB_KEY + " CHAR(66) NOT NULL, " + - B_struct.COMMENT + " VARCHAR(1024), " + + B_struct.COMMENT + " TINYTEXT, " + L_struct.STATUS + " INT NOT NULL, " + L_struct.LOG_TIME + " INT NOT NULL, " + - T_struct.TAG + " VARCHAR (1024), " + + T_struct.TAG + " TINYTEXT, " + T_struct.TAG_TIME + " INT, " + T_struct.TAG_KEY + " CHAR(66), " + T_struct.TAG_SIGN + " VARCHAR(160), " + diff --git a/src/floBlockchainAPI.js b/src/floBlockchainAPI.js index 639fde6..0facffb 100644 --- a/src/floBlockchainAPI.js +++ b/src/floBlockchainAPI.js @@ -41,7 +41,7 @@ //Promised function to get data from API promisedAPI: function(apicall) { return new Promise((resolve, reject) => { - console.log(apicall); + //console.log(apicall); this.util.fetch_api(apicall) .then(result => resolve(result)) .catch(error => reject(error)); @@ -389,7 +389,7 @@ if (options.limit <= 0) options.limit = response.items.length; var filteredData = []; - for (i = 0; i < (response.totalItems - options.ignoreOld) && + for (let i = 0; i < (response.totalItems - options.ignoreOld) && filteredData.length < options.limit; i++) { if (options.sentOnly && response.items[i].vin[0].addr !== addr) continue; diff --git a/src/floGlobals.js b/src/floGlobals.js index 5302453..6bea3e7 100644 --- a/src/floGlobals.js +++ b/src/floGlobals.js @@ -27,4 +27,4 @@ const floGlobals = { }; (typeof global !== "undefined" ? global : window).cryptocoin = floGlobals.blockchain; -('object' === typeof module) ? module.export = floGlobals : null; \ No newline at end of file +('object' === typeof module) ? module.exports = floGlobals : null; \ No newline at end of file diff --git a/src/intra.js b/src/intra.js index ba0ae21..4c7b146 100644 --- a/src/intra.js +++ b/src/intra.js @@ -460,13 +460,13 @@ function sendStoredData(lastlogs, node) { id: n, status: true })); - console.info(`START: ${snID} data sync(send) to ${node.id}`); + console.log(`START: ${snID} data sync(send) to ${node.id}`); //TODO: efficiently handle large number of data instead of loading all into memory result.forEach(d => node.send(packet_.constuct({ type: STORE_BACKUP_DATA, data: d }))); - console.info(`END: ${snID} data sync(send) to ${node.id}`); + console.log(`END: ${snID} data sync(send) to ${node.id}`); node.send(packet_.constuct({ type: DATA_SYNC, id: n, @@ -479,7 +479,7 @@ function sendStoredData(lastlogs, node) { //Indicate sync of data function dataSyncIndication(snID, status, from) { - console.info(`${status ? 'START':'END'}: ${snID} data sync(receive) form ${from}`); + console.log(`${status ? 'START':'END'}: ${snID} data sync(receive) form ${from}`); }; //Store (backup) data @@ -543,7 +543,7 @@ function forwardToNextNode(mode, data) { //Data migration processor function dataMigration(node_change, flag) { - if (!Object.keys(node_change)) + if (!Object.keys(node_change).length) return; console.log("Node list changed! Data migration required"); if (flag) dataMigration.intimateAllNodes(); //Initmate All nodes to call refresher @@ -581,7 +581,7 @@ dataMigration.process_del = async function(del_nodes) { let remaining = process_nodes.length; process_nodes.forEach(n => { DB.getData(n, 0).then(result => { - console.info(`START: Data migration for ${n}`); + console.log(`START: Data migration for ${n}`); //TODO: efficiently handle large number of data instead of loading all into memory result.forEach(d => { let closest = kBucket.closestNode(d.receiverID); @@ -597,7 +597,7 @@ dataMigration.process_del = async function(del_nodes) { data: d })); }); - console.info(`END: Data migration for ${n}`); + console.log(`END: Data migration for ${n}`); _list.delete(n); DB.dropTable(n); remaining--; diff --git a/src/kBucket.js b/src/kBucket.js index fa7aa48..4560ef9 100644 --- a/src/kBucket.js +++ b/src/kBucket.js @@ -1,5 +1,5 @@ 'use strict'; -require('./lib/BuildKBucket') +require('./lib/BuildKBucket'); (function(GLOBAL) { var kBucket = GLOBAL.kBucket = {}; var SNKB, SNCO; @@ -26,7 +26,7 @@ require('./lib/BuildKBucket') }); list.forEach(id => KB.add({ id: decodeID(id), - floID: floID + floID: id })); return KB; }; diff --git a/src/main.js b/src/main.js index bfdb199..471d83a 100644 --- a/src/main.js +++ b/src/main.js @@ -27,6 +27,7 @@ function startNode() { client.DB = DB; client._list = intra._list; loadBase().then(base => { + console.log("Load Database successful"); //Set base data from DB to floGlobals floGlobals.supernodes = base.supernodes; floGlobals.sn_config = base.sn_config; @@ -38,11 +39,11 @@ function startNode() { const server = new Server(config["port"], client, intra); server.refresher = refreshData; intra.refresher = refreshData; - }).catch(error => reject(error)); - }).catch(error => reject(error)); + }).catch(error => console.error(error)); + }).catch(error => console.error(error)); }; -function loadBase(DB) { +function loadBase() { return new Promise((resolve, reject) => { DB.createBase().then(result => { DB.getBase(DB) @@ -57,11 +58,13 @@ const refreshData = { base: null, invoke(flag = true) { this.count = floGlobals.sn_config.refreshDelay; + console.info("Refresher processor has started at " + Date()); refreshBlockchainData(this.base, flag).then(result => { console.log(result); diskCleanUp(this.base) - .then(result => console.info(result)) - .catch(warn => console.warn(warn)); + .then(result => console.log(result)) + .catch(warn => console.warn(warn)) + .finally(_ => console.info("Refresher processor has finished at " + Date())); }).catch(error => console.error(error)); }, get countdown() { @@ -76,7 +79,7 @@ function refreshBlockchainData(base, flag) { readSupernodeConfigFromAPI(base, flag).then(result => { console.log(result); kBucket.launch().then(result => { - console.log(result); + //console.log(result); readAppSubAdminListFromAPI(base) .then(result => console.log(result)) .catch(warn => console.warn(warn)) @@ -139,11 +142,9 @@ function readSupernodeConfigFromAPI(base, flag) { Promise.allSettled(promises).then(results => { if (results.reduce((a, r) => r.status === "rejected" ? ++a : a, 0)) console.warn("Some data might not have been saved in database correctly"); - else - console.log("All data are saved in database"); }); //Process data migration if nodes are changed - if (Object.keys(node_change)) + if (Object.keys(node_change).length) intra.dataMigration(node_change, flag); resolve('Updated Supernode Configuration'); }).catch(error => reject(error)); diff --git a/src/set_globals.js b/src/set_globals.js index 3531535..7a13650 100644 --- a/src/set_globals.js +++ b/src/set_globals.js @@ -5,4 +5,4 @@ global.fetch = require("node-fetch"); //Set browser paramaters from param.json const param = require('../param.json'); for(let p in param) - global[p] = param[p]; \ No newline at end of file + global[p] = param[p];