diff --git a/src/base_tables.json b/src/base_tables.json index 6bf000d..5eacd2d 100644 --- a/src/base_tables.json +++ b/src/base_tables.json @@ -18,7 +18,8 @@ "Applications": { "APP_NAME": "VARCHAR(64) NOT NULL", "ADMIN_ID": "CHAR(34) NOT NULL", - "SUB_ADMINS": "VARCHAR(3500)", + "SUB_ADMINS": "TEXT", + "TRUSTED_IDS": "TEXT", "PRIMARY": "KEY (APP_NAME)" } } \ No newline at end of file diff --git a/src/client.js b/src/client.js index c3c3b0a..5f5dbde 100644 --- a/src/client.js +++ b/src/client.js @@ -109,7 +109,8 @@ function processTagFromUser(data) { if (!(result.application in floGlobals.appList)) return reject(INVALID("Application not authorised")); if (!floCrypto.validateAddr(data.requestorID) || - !floGlobals.appSubAdmins[result.application].includes(data.requestorID)) + (!floGlobals.appSubAdmins[result.application].includes(data.requestorID) + && !floGlobals.appTrustedIDs[result.application].includes(data.requestorID))) return reject(INVALID("Invalid requestorID")); if (!floCrypto.verifyPubKey(data.pubKey, data.requestorID)) return reject(INVALID("Invalid pubKey")); diff --git a/src/database.js b/src/database.js index 5d5f49b..7da3e58 100644 --- a/src/database.js +++ b/src/database.js @@ -96,6 +96,7 @@ DB.setLastTx = function (id, n) { DB.setConfig = function (name, value) { return new Promise((resolve, reject) => { + value = JSON.stringify(value); let statement = "INSERT INTO Configs (NAME, VAL) VALUES (?, ?)" + " ON DUPLICATE KEY UPDATE VAL=?"; queryResolve(statement, [name, value, value]) @@ -144,6 +145,17 @@ DB.setSubAdmin = function (appName, subAdmins) { }); }; +DB.setTrustedIDs = function (appName, trustedIDs) { + return new Promise((resolve, reject) => { + let statement = "UPDATE Applications" + + " SET TRUSTED_IDS=?" + + " WHERE APP_NAME=?"; + queryResolve(statement, [trustedIDs.join(","), appName]) + .then(result => resolve(result)) + .catch(error => reject(error)); + }); +} + DB.addApp = function (appName, adminID) { return new Promise((resolve, reject) => { let statement = "INSERT INTO Applications (APP_NAME, ADMIN_ID) VALUES (?, ?)" + @@ -171,9 +183,10 @@ DB.getBase = function () { 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.sn_config = Object.fromEntries(tmp.Configs.map(a => [a.NAME, JSON.parse(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 ? a.SUB_ADMINS.split(",") : []])); + result.appTrustedIDs = Object.fromEntries(tmp.Applications.map(a => [a.APP_NAME, a.TRUSTED_IDS ? a.TRUSTED_IDS.split(",") : []])); result.supernodes = Object.fromEntries(tmp.SuperNodes.map(a => [a.FLO_ID, { pubKey: a.PUB_KEY, uri: a.URI diff --git a/src/main.js b/src/main.js index c69e4f6..537f15a 100644 --- a/src/main.js +++ b/src/main.js @@ -44,6 +44,7 @@ function startNode() { floGlobals.sn_config = base.sn_config; floGlobals.appList = base.appList; floGlobals.appSubAdmins = base.appSubAdmins; + floGlobals.appTrustedIDs = base.appTrustedIDs; refreshData.base = base; refreshData.invoke(null) .then(_ => intra.reconnectNextNode()).catch(_ => null); @@ -124,7 +125,8 @@ function readSupernodeConfigFromAPI(base, flag) { pattern: "SuperNodeStorage" }).then(result => { let promises = [], - node_change = {}; + node_change = {}, + node_update = new Set(); result.data.reverse().forEach(data => { var content = JSON.parse(data).SuperNodeStorage; if (content.removeNodes) @@ -148,6 +150,12 @@ function readSupernodeConfigFromAPI(base, flag) { else node_change[sn] = true; }; + if (content.updateNodes) + for (let sn in content.updateNodes) { + promises.push(DB.updateSuperNode(sn, content.updateNodes[sn])); + base.supernodes[sn].uri = content.updateNodes[sn]; + node_update.add(sn); + } if (content.config) for (let c in content.config) { promises.push(DB.setConfig(c, content.config[c])); @@ -171,9 +179,11 @@ function readSupernodeConfigFromAPI(base, flag) { console.warn("Some data might not have been saved in database correctly"); }); //Process data migration if nodes are changed - if (Object.keys(node_change).length) { - if (flag === null) - selfDiskMigration(node_change); //When node starts for the 1st time after been inactive, but migration has already taken place. + if (Object.keys(node_change).length || node_update.size) { + if (flag === null) { + if (Object.keys(node_change).length) + selfDiskMigration(node_change); //When node starts for the 1st time after been inactive, but migration has already taken place. + } else intra.dataMigration(node_change, flag); } @@ -193,18 +203,25 @@ function readAppSubAdminListFromAPI(base) { sentOnly: true, pattern: app }).then(result => { - let subAdmins = new Set(base.appSubAdmins[app]); + let subAdmins = new Set(base.appSubAdmins[app]), + trustedIDs = new Set(base.appTrustedIDs[app]); result.data.reverse().forEach(data => { let content = JSON.parse(data)[app]; if (Array.isArray(content.removeSubAdmin)) content.removeSubAdmin.forEach(sa => subAdmins.delete(sa)); if (Array.isArray(content.addSubAdmin)) content.addSubAdmin.forEach(sa => subAdmins.add(sa)); + if (Array.isArray(content.removeTrustedID)) + content.removeTrustedID.forEach(sa => trustedIDs.delete(sa)); + if (Array.isArray(content.addTrustedID)) + content.addTrustedID.forEach(sa => trustedIDs.add(sa)); }); base.appSubAdmins[app] = Array.from(subAdmins); + base.appTrustedIDs[app] = Array.from(trustedIDs); Promise.allSettled([ DB.setLastTx(base.appList[app], result.totalTxs), - DB.setSubAdmin(app, base.appSubAdmins[app]) + DB.setSubAdmin(app, base.appSubAdmins[app]), + DB.setTrustedIDs(app, base.appTrustedIDs[app]) ]).then(results => { if (results.reduce((a, r) => r.status === "rejected" ? ++a : a, 0)) console.warn(`SubAdmin list for app(${app}) might not have been saved in database`);