added getDBHash function in add, update and delete functions

This commit is contained in:
Abhishek Sinha 2019-06-23 17:47:41 +05:30
parent fff9b6dcbe
commit a7a7be2772

View File

@ -10229,11 +10229,17 @@
return arr; return arr;
}, },
getDBHash: async function(su="") { getDBHash: async function(su=localbitcoinplusplus.wallets.my_local_flo_address) {
const tableArray = ["deposit", "withdraw_cash", "withdraw_btc", "cash_balances", "crypto_balances", const tableArray = ["deposit", "withdraw_cash", "withdraw_btc", "cash_balances", "crypto_balances",
"buyOrders", "sellOrders", "system_btc_reserves_private_keys"]; "buyOrders", "sellOrders", "system_btc_reserves_private_keys"];
const dbDataOfSupernode = await localbitcoinplusplus.actions.get_sharable_db_data(tableArray, su); let get_su = await localbitcoinplusplus.kademlia.determineClosestSupernode(su);
const supernode_flo_id = get_su[0].data.id;
const subjectDB = (supernode_flo_id==localbitcoinplusplus.wallets.my_local_flo_address) ? "":supernode_flo_id;
const dbDataOfSupernode = await localbitcoinplusplus.actions.get_sharable_db_data(tableArray, subjectDB);
if (typeof dbDataOfSupernode=="object" && Object.keys(dbDataOfSupernode).length>0) { if (typeof dbDataOfSupernode=="object" && Object.keys(dbDataOfSupernode).length>0) {
myArray = {}; myArray = {};
@ -10243,12 +10249,10 @@
const dbDataOfSupernodeStr = JSON.stringify(dbDataOfSupernode); const dbDataOfSupernodeStr = JSON.stringify(dbDataOfSupernode);
const dbDataOfSupernodeHash = Crypto.SHA256(dbDataOfSupernodeStr); const dbDataOfSupernodeHash = Crypto.SHA256(dbDataOfSupernodeStr);
let data_of = su.length>0 ? su:localbitcoinplusplus.wallets.my_local_flo_address; myArray["id"] = `SU_DB_${subjectDB}`;
myArray["id"] = `SU_DB_${data_of}`;
myArray["DBHash"] = dbDataOfSupernodeHash; myArray["DBHash"] = dbDataOfSupernodeHash;
myArray["trader_flo_address"] = localbitcoinplusplus.wallets.my_local_flo_address; myArray["trader_flo_address"] = localbitcoinplusplus.wallets.my_local_flo_address;
myArray["data_of"] = data_of; myArray["data_of"] = subjectDB;
myArray["timestamp"] = + new Date(); myArray["timestamp"] = + new Date();
const RM_RPC = new localbitcoinplusplus.rpc; const RM_RPC = new localbitcoinplusplus.rpc;
@ -10366,7 +10370,16 @@
const promise2 = removeAllinDB('my_supernode_private_key_chunks'); const promise2 = removeAllinDB('my_supernode_private_key_chunks');
return Promise.all([promise1, promise2]).then(() => true).catch(e => false); return Promise.all([promise1, promise2])
.then(() => {
// Update the DB Hash and inform rest
if (localbitcoinplusplus.master_configurations.supernodesPubKeys
.includes(localbitcoinplusplus.wallets.my_local_flo_public_key)) {
localbitcoinplusplus.actions.getDBHash();
}
return true;
})
.catch(e => false);
}, },
// https://stackoverflow.com/a/39538518/5348972 // https://stackoverflow.com/a/39538518/5348972
@ -10582,7 +10595,14 @@
}, },
updateClosestSupernodeSeeds: function(flo_addr) { updateClosestSupernodeSeeds: function(flo_addr) {
return new Promise(async (resolve, reject) => { return new Promise(async (resolve, reject) => {
await removeAllinDB('myClosestSupernodes'); await removeAllinDB('myClosestSupernodes')
.then(()=>{
// Update the DB Hash and inform rest
if (localbitcoinplusplus.master_configurations.supernodesPubKeys
.includes(localbitcoinplusplus.wallets.my_local_flo_public_key)) {
localbitcoinplusplus.actions.getDBHash();
}
});
let nearestSupernodeAddresslist = await localbitcoinplusplus.kademlia.addClosestSupernodeInDB(flo_addr); let nearestSupernodeAddresslist = await localbitcoinplusplus.kademlia.addClosestSupernodeInDB(flo_addr);
nearestSupernodeAddresslist.map((nearestSupernodeAddress, index)=>{ nearestSupernodeAddresslist.map((nearestSupernodeAddress, index)=>{
updateinDB('myClosestSupernodes', { updateinDB('myClosestSupernodes', {
@ -12511,6 +12531,11 @@
deposit_arr deposit_arr
.trader_flo_address .trader_flo_address
); );
// Update the DB Hash and inform rest
if (localbitcoinplusplus.master_configurations.supernodesPubKeys
.includes(localbitcoinplusplus.wallets.my_local_flo_public_key)) {
localbitcoinplusplus.actions.getDBHash();
}
} }
} }
); );
@ -13810,9 +13835,14 @@
backup_server_db_instance.backup_removeinDB backup_server_db_instance.backup_removeinDB
( (
"deposit", "deposit",
deposit_arr deposit_arr.trader_flo_address
.trader_flo_address ).then(()=>{
); // Update the DB Hash and inform rest
if (localbitcoinplusplus.master_configurations.supernodesPubKeys
.includes(localbitcoinplusplus.wallets.my_local_flo_public_key)) {
localbitcoinplusplus.actions.getDBHash(deposit_arr.trader_flo_address);
}
});
} }
} }
); );
@ -14718,11 +14748,25 @@
_removeinDB( _removeinDB(
"buyOrders", "buyOrders",
buyPipeObj buyPipeObj
.id); .id)
.then(()=>{
// Update the DB Hash and inform rest
if (localbitcoinplusplus.master_configurations.supernodesPubKeys
.includes(localbitcoinplusplus.wallets.my_local_flo_public_key)) {
localbitcoinplusplus.actions.getDBHash(buyPipeObj.trader_flo_address);
}
});
_removeinDB( _removeinDB(
"sellOrders", "sellOrders",
sellPipeObj sellPipeObj
.id); .id)
.then(()=>{
// Update the DB Hash and inform rest
if (localbitcoinplusplus.master_configurations.supernodesPubKeys
.includes(localbitcoinplusplus.wallets.my_local_flo_public_key)) {
localbitcoinplusplus.actions.getDBHash(sellPipeObj.trader_flo_address);
}
});
} catch (error) { } catch (error) {
callback(false); callback(false);
showMessage(`WARNING: Failed to delete respective buy and sell orders in an operation.`); showMessage(`WARNING: Failed to delete respective buy and sell orders in an operation.`);
@ -15800,6 +15844,11 @@
await addDB(resdbdata, obj[prop]); await addDB(resdbdata, obj[prop]);
} }
} }
// Update the DB Hash and inform rest
if (localbitcoinplusplus.master_configurations.supernodesPubKeys
.includes(localbitcoinplusplus.wallets.my_local_flo_public_key)) {
localbitcoinplusplus.actions.getDBHash();
}
} }
} }
@ -15877,7 +15926,14 @@
.verify(cancel_request.trade_id, cancel_request.signed_trade_id, .verify(cancel_request.trade_id, cancel_request.signed_trade_id,
trader_data.trader_flo_pubKey)) { trader_data.trader_flo_pubKey)) {
removeinDB(tradeDB, cancel_request.trade_id) removeinDB(tradeDB, cancel_request.trade_id)
.then((id) => showMessage(`Trade Id ${id} deleted.`)); .then((id) => {
// Update the DB Hash and inform rest
if (localbitcoinplusplus.master_configurations.supernodesPubKeys
.includes(localbitcoinplusplus.wallets.my_local_flo_public_key)) {
localbitcoinplusplus.actions.getDBHash();
}
showMessage(`Trade Id ${id} deleted.`);
});
} else { } else {
showMessage( showMessage(
`Failed to verify trade for trade id ${cancel_request.trade_id}` `Failed to verify trade for trade id ${cancel_request.trade_id}`
@ -15910,8 +15966,20 @@
trade_balance_res.supernode_sign, trade_balance_res.supernodePubKey)) { trade_balance_res.supernode_sign, trade_balance_res.supernodePubKey)) {
// Delete orders in clients DB // Delete orders in clients DB
try { try {
removeinDB("buyOrders", trade_balance_res.trade_infos.buy_order_id); removeinDB("buyOrders", trade_balance_res.trade_infos.buy_order_id).then(()=>{
removeinDB("sellOrders", trade_balance_res.trade_infos.sell_order_id); // Update the DB Hash and inform rest
if (localbitcoinplusplus.master_configurations.supernodesPubKeys
.includes(localbitcoinplusplus.wallets.my_local_flo_public_key)) {
localbitcoinplusplus.actions.getDBHash();
}
});
removeinDB("sellOrders", trade_balance_res.trade_infos.sell_order_id).then(()=>{
// Update the DB Hash and inform rest
if (localbitcoinplusplus.master_configurations.supernodesPubKeys
.includes(localbitcoinplusplus.wallets.my_local_flo_public_key)) {
localbitcoinplusplus.actions.getDBHash();
}
});
} catch (error) { } catch (error) {
callback(false); callback(false);
throw new Error(error); throw new Error(error);
@ -16106,9 +16174,21 @@
removeByIndex('deposit', removeByIndex('deposit',
'trader_flo_address', 'trader_flo_address',
depositor_cash_data.trader_flo_address depositor_cash_data.trader_flo_address
); ).then(()=>{
removeinDB('withdraw_cash', // Update the DB Hash and inform rest
withdraw_data.id); if (localbitcoinplusplus.master_configurations.supernodesPubKeys
.includes(localbitcoinplusplus.wallets.my_local_flo_public_key)) {
localbitcoinplusplus.actions.getDBHash();
}
});
removeinDB('withdraw_cash', withdraw_data.id)
.then(()=>{
// Update the DB Hash and inform rest
if (localbitcoinplusplus.master_configurations.supernodesPubKeys
.includes(localbitcoinplusplus.wallets.my_local_flo_public_key)) {
localbitcoinplusplus.actions.getDBHash();
}
});
let update_cash_balance_obj = { let update_cash_balance_obj = {
depositor_cash_data: depositor_cash_data, depositor_cash_data: depositor_cash_data,
@ -16232,8 +16312,20 @@
updateinDB('cash_balances', withdraw_success_response.depositor_cash_data); updateinDB('cash_balances', withdraw_success_response.depositor_cash_data);
updateinDB('cash_balances', withdraw_success_response.withdrawer_cash_data); updateinDB('cash_balances', withdraw_success_response.withdrawer_cash_data);
removeByIndex('deposit', 'trader_flo_address', withdraw_success_response.depositor_cash_data removeByIndex('deposit', 'trader_flo_address', withdraw_success_response.depositor_cash_data
.trader_flo_address); .trader_flo_address).then(()=>{
removeinDB('withdraw_cash', withdraw_success_response.withdraw_id); // Update the DB Hash and inform rest
if (localbitcoinplusplus.master_configurations.supernodesPubKeys
.includes(localbitcoinplusplus.wallets.my_local_flo_public_key)) {
localbitcoinplusplus.actions.getDBHash();
}
});
removeinDB('withdraw_cash', withdraw_success_response.withdraw_id).then(()=>{
// Update the DB Hash and inform rest
if (localbitcoinplusplus.master_configurations.supernodesPubKeys
.includes(localbitcoinplusplus.wallets.my_local_flo_public_key)) {
localbitcoinplusplus.actions.getDBHash();
}
});
return true; return true;
} }
return false; return false;
@ -16576,6 +16668,11 @@
} }
} }
} }
// Update the DB Hash and inform rest
if (localbitcoinplusplus.master_configurations.supernodesPubKeys
.includes(localbitcoinplusplus.wallets.my_local_flo_public_key)) {
localbitcoinplusplus.actions.getDBHash(su_db_data.trader_flo_address);
}
} }
} catch (error) { } catch (error) {
@ -16702,6 +16799,11 @@
}); });
} }
} }
// Update the DB Hash and inform rest
if (localbitcoinplusplus.master_configurations.supernodesPubKeys
.includes(localbitcoinplusplus.wallets.my_local_flo_public_key)) {
localbitcoinplusplus.actions.getDBHash();
}
} }
} }
@ -16792,6 +16894,11 @@
showMessage(`INFO: "${resdbdata}" datastore syncing is complete.`); showMessage(`INFO: "${resdbdata}" datastore syncing is complete.`);
}); });
} }
// Update the DB Hash and inform rest
if (localbitcoinplusplus.master_configurations.supernodesPubKeys
.includes(localbitcoinplusplus.wallets.my_local_flo_public_key)) {
localbitcoinplusplus.actions.getDBHash(su_db_data.trader_flo_address);
}
} }
} }
} }
@ -17101,6 +17208,11 @@
await addDB(resdbdata, obj[prop]); await addDB(resdbdata, obj[prop]);
} }
} }
// Update the DB Hash and inform rest
if (localbitcoinplusplus.master_configurations.supernodesPubKeys
.includes(localbitcoinplusplus.wallets.my_local_flo_public_key)) {
localbitcoinplusplus.actions.getDBHash();
}
} }
} }
@ -17196,7 +17308,14 @@
.verify(cancel_request.trade_id, cancel_request.signed_trade_id, .verify(cancel_request.trade_id, cancel_request.signed_trade_id,
trader_data.trader_flo_pubKey)) { trader_data.trader_flo_pubKey)) {
backup_server_db_instance.backup_removeinDB(tradeDB, cancel_request.trade_id) backup_server_db_instance.backup_removeinDB(tradeDB, cancel_request.trade_id)
.then((id) => showMessage(`Trade Id ${id} deleted.`)); .then((id) => {
// Update the DB Hash and inform rest
if (localbitcoinplusplus.master_configurations.supernodesPubKeys
.includes(localbitcoinplusplus.wallets.my_local_flo_public_key)) {
localbitcoinplusplus.actions.getDBHash(trader_data.trader_flo_address);
}
showMessage(`Trade Id ${id} deleted.`);
});
} else { } else {
showMessage( showMessage(
`Failed to verify trade for trade id ${cancel_request.trade_id}` `Failed to verify trade for trade id ${cancel_request.trade_id}`
@ -17231,8 +17350,22 @@
// Delete orders in clients DB // Delete orders in clients DB
try { try {
removeinDB("buyOrders", trade_balance_res.trade_infos.buy_order_id); removeinDB("buyOrders", trade_balance_res.trade_infos.buy_order_id)
removeinDB("sellOrders", trade_balance_res.trade_infos.sell_order_id); .then(()=>{
// Update the DB Hash and inform rest
if (localbitcoinplusplus.master_configurations.supernodesPubKeys
.includes(localbitcoinplusplus.wallets.my_local_flo_public_key)) {
localbitcoinplusplus.actions.getDBHash();
}
});
removeinDB("sellOrders", trade_balance_res.trade_infos.sell_order_id)
.then(()=>{
// Update the DB Hash and inform rest
if (localbitcoinplusplus.master_configurations.supernodesPubKeys
.includes(localbitcoinplusplus.wallets.my_local_flo_public_key)) {
localbitcoinplusplus.actions.getDBHash();
}
});
} catch (error) { } catch (error) {
callback(false); callback(false);
throw new Error(error); throw new Error(error);
@ -17504,9 +17637,21 @@
backup_server_db_instance.backup_removeByIndex('deposit', backup_server_db_instance.backup_removeByIndex('deposit',
'trader_flo_address', 'trader_flo_address',
depositor_cash_data.trader_flo_address depositor_cash_data.trader_flo_address
); ).then(()=>{
backup_server_db_instance.backup_removeinDB('withdraw_cash', // Update the DB Hash and inform rest
withdraw_data.id); if (localbitcoinplusplus.master_configurations.supernodesPubKeys
.includes(localbitcoinplusplus.wallets.my_local_flo_public_key)) {
localbitcoinplusplus.actions.getDBHash(depositor_cash_data.trader_flo_address);
}
});
backup_server_db_instance.backup_removeinDB('withdraw_cash', withdraw_data.id)
.then(()=>{
// Update the DB Hash and inform rest
if (localbitcoinplusplus.master_configurations.supernodesPubKeys
.includes(localbitcoinplusplus.wallets.my_local_flo_public_key)) {
localbitcoinplusplus.actions.getDBHash(withdraw_data.trader_flo_address);
}
});
let update_cash_balance_obj = { let update_cash_balance_obj = {
depositor_cash_data: depositor_cash_data, depositor_cash_data: depositor_cash_data,
@ -17638,8 +17783,21 @@
updateinDB('cash_balances', withdraw_success_response.depositor_cash_data); updateinDB('cash_balances', withdraw_success_response.depositor_cash_data);
updateinDB('cash_balances', withdraw_success_response.withdrawer_cash_data); updateinDB('cash_balances', withdraw_success_response.withdrawer_cash_data);
removeByIndex('deposit', 'trader_flo_address', withdraw_success_response.depositor_cash_data removeByIndex('deposit', 'trader_flo_address', withdraw_success_response.depositor_cash_data
.trader_flo_address); .trader_flo_address).then(()=>{
removeinDB('withdraw_cash', withdraw_success_response.withdraw_id); // Update the DB Hash and inform rest
if (localbitcoinplusplus.master_configurations.supernodesPubKeys
.includes(localbitcoinplusplus.wallets.my_local_flo_public_key)) {
localbitcoinplusplus.actions.getDBHash();
}
});
removeinDB('withdraw_cash', withdraw_success_response.withdraw_id)
.then(()=>{
// Update the DB Hash and inform rest
if (localbitcoinplusplus.master_configurations.supernodesPubKeys
.includes(localbitcoinplusplus.wallets.my_local_flo_public_key)) {
localbitcoinplusplus.actions.getDBHash();
}
});
return true; return true;
} }
return false; return false;
@ -18024,6 +18182,11 @@
await BACKUP_DB.backup_addDB(resdbdata, obj[prop]); await BACKUP_DB.backup_addDB(resdbdata, obj[prop]);
} }
} }
// Update the DB Hash and inform rest
if (localbitcoinplusplus.master_configurations.supernodesPubKeys
.includes(localbitcoinplusplus.wallets.my_local_flo_public_key)) {
localbitcoinplusplus.actions.getDBHash(su_db_data.trader_flo_address);
}
} }
} }
@ -18422,6 +18585,11 @@
await addDB(resdbdata, obj[prop]); await addDB(resdbdata, obj[prop]);
} }
} }
// Update the DB Hash and inform rest
if (localbitcoinplusplus.master_configurations.supernodesPubKeys
.includes(localbitcoinplusplus.wallets.my_local_flo_public_key)) {
localbitcoinplusplus.actions.getDBHash();
}
} }
} }
@ -18599,7 +18767,14 @@
.verify(cancel_request.trade_id, cancel_request.signed_trade_id, .verify(cancel_request.trade_id, cancel_request.signed_trade_id,
trader_data.trader_flo_pubKey)) { trader_data.trader_flo_pubKey)) {
backup_server_db_instance.backup_removeinDB(tradeDB, cancel_request.trade_id) backup_server_db_instance.backup_removeinDB(tradeDB, cancel_request.trade_id)
.then((id) => showMessage(`Trade Id ${id} deleted.`)); .then((id) => {
// Update the DB Hash and inform rest
if (localbitcoinplusplus.master_configurations.supernodesPubKeys
.includes(localbitcoinplusplus.wallets.my_local_flo_public_key)) {
localbitcoinplusplus.actions.getDBHash(cancel_request.trader_flo_address);
}
showMessage(`Trade Id ${id} deleted.`);
});
} else { } else {
showMessage( showMessage(
`Failed to verify trade for trade id ${cancel_request.trade_id}` `Failed to verify trade for trade id ${cancel_request.trade_id}`
@ -18651,8 +18826,22 @@
// Delete orders in clients DB // Delete orders in clients DB
try { try {
backup_server_db_instance.backup_removeinDB("buyOrders", trade_balance_res.trade_infos.buy_order_id); backup_server_db_instance.backup_removeinDB("buyOrders", trade_balance_res.trade_infos.buy_order_id)
backup_server_db_instance.backup_removeinDB("sellOrders", trade_balance_res.trade_infos.sell_order_id); .then(()=>{
// Update the DB Hash and inform rest
if (localbitcoinplusplus.master_configurations.supernodesPubKeys
.includes(localbitcoinplusplus.wallets.my_local_flo_public_key)) {
localbitcoinplusplus.actions.getDBHash(trade_balance_res.trade_infos.buyer_flo_id);
}
});
backup_server_db_instance.backup_removeinDB("sellOrders", trade_balance_res.trade_infos.sell_order_id)
.then(()=>{
// Update the DB Hash and inform rest
if (localbitcoinplusplus.master_configurations.supernodesPubKeys
.includes(localbitcoinplusplus.wallets.my_local_flo_public_key)) {
localbitcoinplusplus.actions.getDBHash(trade_balance_res.trade_infos.seller_flo_id);
}
});
} catch (error) { } catch (error) {
callback(false); callback(false);
throw new Error(error); throw new Error(error);
@ -18755,8 +18944,21 @@
backup_server_db_instance.backup_updateinDB('cash_balances', withdraw_success_response.depositor_cash_data); backup_server_db_instance.backup_updateinDB('cash_balances', withdraw_success_response.depositor_cash_data);
backup_server_db_instance.backup_updateinDB('cash_balances', withdraw_success_response.withdrawer_cash_data); backup_server_db_instance.backup_updateinDB('cash_balances', withdraw_success_response.withdrawer_cash_data);
backup_server_db_instance.backup_removeByIndex('deposit', 'trader_flo_address', withdraw_success_response.depositor_cash_data backup_server_db_instance.backup_removeByIndex('deposit', 'trader_flo_address', withdraw_success_response.depositor_cash_data
.trader_flo_address); .trader_flo_address).then(()=>{
backup_server_db_instance.backup_removeinDB('withdraw_cash', withdraw_success_response.withdraw_id); // Update the DB Hash and inform rest
if (localbitcoinplusplus.master_configurations.supernodesPubKeys
.includes(localbitcoinplusplus.wallets.my_local_flo_public_key)) {
localbitcoinplusplus.actions.getDBHash(withdraw_success_response.depositor_cash_data.trader_flo_address);
}
});
backup_server_db_instance.backup_removeinDB('withdraw_cash', withdraw_success_response.withdraw_id)
.then(()=>{
// Update the DB Hash and inform rest
if (localbitcoinplusplus.master_configurations.supernodesPubKeys
.includes(localbitcoinplusplus.wallets.my_local_flo_public_key)) {
localbitcoinplusplus.actions.getDBHash(withdraw_success_response.trader_flo_address);
}
});
return true; return true;
} }
return false; return false;
@ -19094,27 +19296,27 @@
showMessage(backup_db_error_msg); showMessage(backup_db_error_msg);
throw new Error(backup_db_error_msg); throw new Error(backup_db_error_msg);
}; };
});
let updateUserDepositsResponseString = JSON.stringify( let updateUserDepositsResponseString = JSON.stringify(
updateUserDepositsResponseObject.updatedTraderDepositObject); updateUserDepositsResponseObject.updatedTraderDepositObject);
let updateUserDepositsResponseStringHash = Crypto.SHA256(updateUserDepositsResponseString); let updateUserDepositsResponseStringHash = Crypto.SHA256(updateUserDepositsResponseString);
let isBalanceLegit = RM_WALLET.verify(updateUserDepositsResponseStringHash, let isBalanceLegit = RM_WALLET.verify(updateUserDepositsResponseStringHash,
updateUserDepositsResponseObject.updatedDepositsObjectSign, updateUserDepositsResponseObject.updatedDepositsObjectSign,
res_obj.nodePubKey res_obj.nodePubKey
); );
if (isBalanceLegit) { if (isBalanceLegit) {
backup_server_db_instance.backup_updateinDB("deposits", backup_server_db_instance.backup_updateinDB("deposits",
updateUserDepositsResponseObject.updatedTraderDepositObject); updateUserDepositsResponseObject.updatedTraderDepositObject);
if (localbitcoinplusplus.wallets.my_local_flo_address == if (localbitcoinplusplus.wallets.my_local_flo_address ==
updateUserDepositsResponseObject.trader_flo_address) { updateUserDepositsResponseObject.trader_flo_address) {
displayBalances(updateUserDepositsResponseObject.trader_flo_address); displayBalances(updateUserDepositsResponseObject.trader_flo_address);
showMessage(`INFO: Your balance is updated.`); showMessage(`INFO: Your balance is updated.`);
} }
return true; return true;
} else { } else {
showMessage(`WARNING: Failed to update balance in your DB. Please refresh.`); showMessage(`WARNING: Failed to update balance in your DB. Please refresh.`);
} }
});
} }
break; break;
@ -19123,38 +19325,38 @@
.includes(res_obj.nodePubKey)) { .includes(res_obj.nodePubKey)) {
let updateUserReservesResponseObject = res_obj.params[0]; let updateUserReservesResponseObject = res_obj.params[0];
let backup_server_db_instance = "";
if(typeof res_obj.params[0].trader_flo_address !="string") return; if(typeof res_obj.params[0].trader_flo_address !="string") return;
localbitcoinplusplus.kademlia.determineClosestSupernode(res_obj.params[0].trader_flo_address) localbitcoinplusplus.kademlia.determineClosestSupernode(res_obj.params[0].trader_flo_address)
.then(my_closest_su_list=>{ .then(my_closest_su_list=>{
const primarySupernodeOfThisUser = my_closest_su_list[0].data.id; const primarySupernodeOfThisUser = my_closest_su_list[0].data.id;
const backup_server_db_instance = localbitcoinplusplus.newBackupDatabase.db[primarySupernodeOfThisUser]; backup_server_db_instance = localbitcoinplusplus.newBackupDatabase.db[primarySupernodeOfThisUser];
if(typeof backup_server_db_instance !== "object") { if(typeof backup_server_db_instance !== "object") {
let backup_db_error_msg = `WARNING: Unknown DB instance. DB Backup failed.`; let backup_db_error_msg = `WARNING: Unknown DB instance. DB Backup failed.`;
showMessage(backup_db_error_msg); showMessage(backup_db_error_msg);
throw new Error(backup_db_error_msg); throw new Error(backup_db_error_msg);
}; };
});
let updateUserReservesResponseString = JSON.stringify( let updateUserReservesResponseString = JSON.stringify(
updateUserReservesResponseObject.updatedReservesObject); updateUserReservesResponseObject.updatedReservesObject);
let updateUserReservesResponseStringHash = Crypto.SHA256(updateUserReservesResponseString); let updateUserReservesResponseStringHash = Crypto.SHA256(updateUserReservesResponseString);
let isBalanceLegit = RM_WALLET.verify(updateUserReservesResponseStringHash, let isBalanceLegit = RM_WALLET.verify(updateUserReservesResponseStringHash,
updateUserReservesResponseObject.updatedBTCReservesObjectSign, updateUserReservesResponseObject.updatedBTCReservesObjectSign,
res_obj.nodePubKey res_obj.nodePubKey
); );
if (isBalanceLegit) { if (isBalanceLegit) {
backup_server_db_instance.backup_updateinDB("system_btc_reserves_private_keys", updateUserReservesResponseObject.updatedReservesObject); backup_server_db_instance.backup_updateinDB("system_btc_reserves_private_keys", updateUserReservesResponseObject.updatedReservesObject);
if (localbitcoinplusplus.wallets.my_local_flo_address == if (localbitcoinplusplus.wallets.my_local_flo_address ==
updateUserReservesResponseObject.trader_flo_address) { updateUserReservesResponseObject.trader_flo_address) {
displayBalances(updateUserReservesResponseObject.trader_flo_address); displayBalances(updateUserReservesResponseObject.trader_flo_address);
showMessage(`INFO: Your balance is updated.`); showMessage(`INFO: Your balance is updated.`);
} }
return true; return true;
} else { } else {
showMessage(`WARNING: Failed to update balance in your DB. Please refresh.`); showMessage(`WARNING: Failed to update balance in your DB. Please refresh.`);
} }
});
} }
break; break;
@ -19211,6 +19413,11 @@
showMessage(`INFO: "${resdbdata}" datastore syncing is complete.`); showMessage(`INFO: "${resdbdata}" datastore syncing is complete.`);
}); });
} }
// Update the DB Hash and inform rest
if (localbitcoinplusplus.master_configurations.supernodesPubKeys
.includes(localbitcoinplusplus.wallets.my_local_flo_public_key)) {
localbitcoinplusplus.actions.getDBHash(su_db_data.trader_flo_address);
}
} }
} }
} }
@ -19223,6 +19430,41 @@
} }
break; break;
case "hash_of_a_supernode_db_response":
if (localbitcoinplusplus.master_configurations.supernodesPubKeys
.includes(res_obj.nodePubKey)) {
const response_object = res_obj.params[0];
if(typeof response_object.trader_flo_address !="string") return;
localbitcoinplusplus.kademlia.determineClosestSupernode(res_obj.params[0].trader_flo_address)
.then(my_closest_su_list=>{
const primarySupernodeOfThisUser = my_closest_su_list[0].data.id;
const backup_server_db_instance = localbitcoinplusplus.newBackupDatabase.db[primarySupernodeOfThisUser];
if(typeof backup_server_db_instance !== "object"
|| backup_server_db_instance==localbitcoinplusplus.wallets.my_local_flo_address) {
let backup_db_error_msg = `WARNING: Unknown DB instance. DB Backup failed.`;
showMessage(backup_db_error_msg);
throw new Error(backup_db_error_msg);
};
backup_server_db_instance.backup_readDB('supernodesDbHash', response_object.id)
.then(old_db_hash_obj=>{
if(typeof old_db_hash_obj=="object") {
if((old_db_hash_obj.vectorClock>=response_object.vectorClock)
|| (old_db_hash_obj.timestamp>=response_object.timestamp)) return;
}
backup_server_db_instance.backup_updateinDB('supernodesDbHash', response_object)
.then(()=>showMessage(`INFO: Datastore "supernodesDbHash" updated
with hash ${response_object.DBHash} on ${response_object.timestamp}.`));
});
});
}
break;
default: default:
break; break;
} }
@ -19496,19 +19738,9 @@
temporary_ip: null temporary_ip: null
} }
const closestSupernodesTable = {
id: null,
ip: null,
is_live: null,
port: null,
timestamp: null,
trader_flo_address: null,
vectorClock: null
}
var db; var db;
const DBName = "localbitcoinDB"; const DBName = "localbitcoinDB";
const request = window.indexedDB.open(DBName, 1); const request = window.indexedDB.open(DBName, 2);
request.onerror = function (event) { request.onerror = function (event) {
//https://stackoverflow.com/questions/13972385/invalidstateerror-while-opening-indexeddb-in-firefox //https://stackoverflow.com/questions/13972385/invalidstateerror-while-opening-indexeddb-in-firefox
@ -19691,14 +19923,6 @@
unique: false unique: false
}); });
} }
if (!db.objectStoreNames.contains('closestSupernodesTable')) {
var objectStore = db.createObjectStore("closestSupernodesTable", {
keyPath: 'id'
});
objectStore.createIndex('trader_flo_address', 'trader_flo_address', {
unique: false
});
}
if (!db.objectStoreNames.contains('supernodesDbHash')) { if (!db.objectStoreNames.contains('supernodesDbHash')) {
var objectStore = db.createObjectStore("supernodesDbHash", { var objectStore = db.createObjectStore("supernodesDbHash", {
keyPath: 'id' keyPath: 'id'
@ -19782,6 +20006,11 @@
await store.add(dbObject); await store.add(dbObject);
await request.complete; await request.complete;
console.info("Data added in " + tablename); console.info("Data added in " + tablename);
// Update the DB Hash and inform rest
if (localbitcoinplusplus.master_configurations.supernodesPubKeys
.includes(localbitcoinplusplus.wallets.my_local_flo_public_key)) {
localbitcoinplusplus.actions.getDBHash();
}
return dbObject; return dbObject;
} catch (error) { } catch (error) {
return new Error(error); return new Error(error);
@ -19814,20 +20043,22 @@
Obj.vectorClock = 1; Obj.vectorClock = 1;
await store.put(Obj); await store.put(Obj);
await request.complete; await request.complete;
return Obj; } else if (myRecord.vectorClock+1 < Obj.vectorClock) {
}
else if (myRecord.vectorClock+1 < Obj.vectorClock) {
await store.put(Obj); await store.put(Obj);
await request.complete; await request.complete;
return Obj;
} }
} }
} else { } else {
await store.put(Obj); await store.put(Obj);
await request.complete; await request.complete;
return Obj;
} }
// Update the DB Hash and inform rest
if (localbitcoinplusplus.master_configurations.supernodesPubKeys
.includes(localbitcoinplusplus.wallets.my_local_flo_public_key)) {
localbitcoinplusplus.actions.getDBHash();
}
return Obj;
} catch (error) { } catch (error) {
return new Error(error); return new Error(error);
@ -20144,6 +20375,13 @@
await store.add(dbObject); await store.add(dbObject);
await this.request.complete; await this.request.complete;
console.info("Data added in " + tablename); console.info("Data added in " + tablename);
// Update the DB Hash and inform rest
if (localbitcoinplusplus.master_configurations.supernodesPubKeys
.includes(localbitcoinplusplus.wallets.my_local_flo_public_key)) {
if (typeof dbObject.trader_flo_address=="string") {
localbitcoinplusplus.actions.getDBHash(dbObject.trader_flo_address);
}
}
return dbObject; return dbObject;
} catch (error) { } catch (error) {
return new Error(error); return new Error(error);
@ -20178,20 +20416,35 @@
Obj.vectorClock = 1; Obj.vectorClock = 1;
await store.put(Obj); await store.put(Obj);
await that.request.complete; await that.request.complete;
resolve(Obj);
return;
} }
else if (myRecord.vectorClock+1 < Obj.vectorClock) { else if (myRecord.vectorClock+1 < Obj.vectorClock) {
await store.put(Obj); await store.put(Obj);
await that.request.complete; await that.request.complete;
resolve(Obj);
} }
}); });
// Update the DB Hash and inform rest
if (localbitcoinplusplus.master_configurations.supernodesPubKeys
.includes(localbitcoinplusplus.wallets.my_local_flo_public_key)) {
if (typeof dbObject.trader_flo_address=="string") {
localbitcoinplusplus.actions.getDBHash(dbObject.trader_flo_address);
}
}
resolve(Obj);
return;
} }
} else { } else {
await store.put(Obj); await store.put(Obj);
await that.request.complete; await that.request.complete;
// Update the DB Hash and inform rest
if (localbitcoinplusplus.master_configurations.supernodesPubKeys
.includes(localbitcoinplusplus.wallets.my_local_flo_public_key)) {
if (typeof dbObject.trader_flo_address=="string") {
localbitcoinplusplus.actions.getDBHash(dbObject.trader_flo_address);
}
}
return Obj; return Obj;
} }
@ -20231,21 +20484,21 @@
request.onerror = function (e) { request.onerror = function (e) {
reject(e); reject(e);
} }
}) });
}, },
async backup_removeAllinDB(tablename) { async backup_removeAllinDB(tablename) {
try { try {
this.request = this.db.transaction([tablename], "readwrite") this.request = this.db.transaction([tablename], "readwrite")
var objectStore = this.request.objectStore(tablename); var objectStore = this.request.objectStore(tablename);
var objectStoreRequest = await objectStore.clear(); var objectStoreRequest = await objectStore.clear();
await this.request.complete; await this.request.complete;
console.info("All the data entry has been removed from your database " + tablename); console.info("All the data entry has been removed from your database " + tablename);
return tablename; return tablename;
} catch (error) { } catch (error) {
return new Error(error); return new Error(error);
}
} }
}
} }
</script> </script>