diff --git a/supernode/index.html b/supernode/index.html index 49d75ba..844c396 100644 --- a/supernode/index.html +++ b/supernode/index.html @@ -8954,6 +8954,46 @@ return false; }); }, + + sync_with_supernode: function(trader_flo_address) { + let sync_request = localbitcoinplusplus.rpc.prototype.send_rpc.call(this, + "sync_with_supernode", { + "trader_flo_address": trader_flo_address, + "job": "SYNC_MY_LOCAL_DB_WITH_SUPERNODE_DB" + }); + doSend(sync_request); + }, + + get_sharable_db_data: function(callback) { + let sharable_data = {}; + + readAllDB("buyOrders", function(buyOrdersres) { + sharable_data.buyOrders = buyOrdersres; + + readAllDB("sellOrders", function(sellOrdersres) { + sharable_data.sellOrders = sellOrdersres; + + readAllDB("btc_balances", function(btc_balancesres) { + sharable_data.btc_balances = btc_balancesres; + + readAllDB("cash_balances", function(cash_balancesres) { + sharable_data.cash_balances = cash_balancesres; + + readAllDB("deposit", function(depositres) { + sharable_data.deposit = depositres; + + readAllDB("withdraw_cash", function(withdraw_cashres) { + sharable_data.withdraw_cash = withdraw_cashres; + return callback(sharable_data); + }); + }); + }); + }); + }); + }); + + } + } @@ -9188,20 +9228,22 @@ ); }); break; - case "broadcastBlockDataToAll": - // TODO: Make a separate class for syncing - if (typeof params.blockOwnerFLOAddress !== undefined) { - request.response = updateinDB("datablocks", params, params.id); - - // Update client nodes HTML for dataBlocks DB - readDB("datablocks", "00-01", function (blockData) { - document.getElementById("datablocksdiv").innerHTML = - `
Version: ${blockData.version}
-Block Owner FLO Address: ${blockData.blockOwnerFLOAddress}
-Block Signature: ${blockData.blockSignature}
-Blockhash: ${blockData.blockhash}
` - }); - } + case "sync_with_supernode": + localbitcoinplusplus.rpc.prototype.filter_legit_requests(function (is_valid_request) { + if (is_valid_request === true && params.job=="SYNC_MY_LOCAL_DB_WITH_SUPERNODE_DB" && params.trader_flo_address.length>0) { + localbitcoinplusplus.actions.get_sharable_db_data(function(su_db_data) { + if (typeof su_db_data == "object") { + let server_sync_response = localbitcoinplusplus.rpc.prototype + .send_rpc + .call(this, "server_sync_response", + su_db_data); + doSend(server_sync_response); + return; + } + }); + } + }); + break; case "deposit_asset_request": localbitcoinplusplus.rpc.prototype.filter_legit_requests(function (is_valid_request) { @@ -10666,11 +10708,29 @@ } } break; - case "broadcastBlockDataToAll": + case "sync_with_supernode": response_from_sever = localbitcoinplusplus.rpc.prototype.receive_rpc_response.call(this, JSON.stringify(res_obj)); - console.log(response_from_sever); - //doSend(response_from_sever); // send response to client + break; + case "server_sync_response": + if (typeof res_obj.params == "object" && typeof res_obj.params[0] == "object") { + let su_db_data = res_obj.params[0]; + for (var tableStoreName in su_db_data) { + // skip loop if the property is from prototype + if (!su_db_data.hasOwnProperty(tableStoreName)) continue; + + var obj = su_db_data[tableStoreName]; + + removeAllinDB(tableStoreName, function(res) { + if (res!==false) { + for (var prop in obj) { + if(!obj.hasOwnProperty(prop)) continue; + addDB(tableStoreName, obj[prop]); + } + } + }); + } + } break; case "deposit_asset_request": response_from_sever = localbitcoinplusplus.rpc.prototype.receive_rpc_response.call(this, @@ -11110,6 +11170,22 @@ console.error(event); } } + + function removeAllinDB(tablename, callback) { + var request = db.transaction([tablename], "readwrite") + var objectStore = request.objectStore(tablename); + var objectStoreRequest = objectStore.clear(); + objectStoreRequest.onsuccess = function (event) { + console.info("All the data entry has been removed from your database "+tablename); + callback(tablename); + //callback(true); + }; + + objectStoreRequest.onerror = function (event) { + console.error(event); + callback(false); + } + } @@ -11165,7 +11241,7 @@ undefined) { localbitcoinplusplusObj.myLocalFLOAddress = newKeys.address; localbitcoinplusplusObj.myLocalFLOPublicKey = newKeys.pubKeyHex; - localbitcoinplusplusObj.myLocalFLOPrivateKey = newKeys.privateKeyWIF; + //localbitcoinplusplusObj.myLocalFLOPrivateKey = newKeys.privateKeyWIF; localbitcoinplusplusObj.mySelfDeclaredBalance = 0; localbitcoinplusplusObj.mySelfDeclaredBalanceFLO = 0; localbitcoinplusplusObj.mySelfdeclaredBalanceBitcoin = 0; @@ -11176,6 +11252,9 @@ } } + // Declare the user flo address + const MY_LOCAL_FLO_ADDRESS = localbitcoinplusplus.wallets.my_local_flo_address = idbData.myLocalFLOAddress; + // rebuild private key let supernode_transaction_key_arr = []; if (localbitcoinplusplus.master_configurations.supernodesPubKeys.includes(idbData.myLocalFLOPublicKey)) { @@ -11199,6 +11278,8 @@ }); } + localbitcoinplusplus.actions.sync_with_supernode(MY_LOCAL_FLO_ADDRESS); + //localbitcoinuserdiv document.getElementById("localbitcoinuserdiv").innerHTML = `Address: ${idbData.myLocalFLOAddress}
@@ -11276,69 +11357,6 @@ } - - -