From d13cceeb33924b73cd2d7b2be1f2eb229f1f1b81 Mon Sep 17 00:00:00 2001 From: Abhishek Sinha Date: Thu, 12 Sep 2019 21:19:15 +0530 Subject: [PATCH 1/4] fixed backup ws connection loop issue --- supernode/index.html | 102 ++++++++++++++++++++----------------------- 1 file changed, 47 insertions(+), 55 deletions(-) diff --git a/supernode/index.html b/supernode/index.html index 68d574e..e0a59be 100644 --- a/supernode/index.html +++ b/supernode/index.html @@ -13225,10 +13225,9 @@ Event information log reactor.registerEvent("shift_ws_connection"); reactor.registerEvent("switchToBackupWSForSuperNodesOperations"); reactor.registerEvent("user_flo_keys_active"); + reactor.registerEvent("backup_supernode_down"); reactor.registerEvent("remove_extra_backup_connections"); - reactor.registerEvent( - "request_to_modify_user_balance_on_successfull_fiat_deposit" - ); + reactor.addEventListener("fireNodeWelcomeBackEvent", function(evt) { let getFLOId = bitjs.FLO_TEST.pubkey2address(evt.flo_public_key); @@ -13738,21 +13737,23 @@ Event information log localbitcoinplusplus.backupWS[getFLOId].ws_connection .readyState >= 2 ) { - localbitcoinplusplus.backupWS[getFLOId] = null; - localbitcoinplusplus.backupWS[ - getFLOId - ] = new backupSupernodesWebSocketObject(back_ws_url); - localbitcoinplusplus.backupWS[getFLOId].connectWS(); - /* Now in connectWS if connection fails call switchToBackupWSForSuperNodesOperations() in onClose() + if (getFLOId !== localbitcoinplusplus.wallets.my_local_flo_address) { + localbitcoinplusplus.backupWS[getFLOId] = null; + localbitcoinplusplus.backupWS[ + getFLOId + ] = new backupSupernodesWebSocketObject(back_ws_url); + localbitcoinplusplus.backupWS[getFLOId].connectWS(); + /* Now in connectWS if connection fails call switchToBackupWSForSuperNodesOperations() in onClose() for next su node, if its success check for max baxkup connection conditon, if required - execute 'resolve_backup_ws_connections' in onOpen() or exit */ + execute 'resolve_backup_ws_connections' in onOpen() or exit */ + } } } catch (error) { console.error(error); - reactor.dispatchEvent( - "switchToBackupWSForSuperNodesOperations", - back_ws_url - ); + // reactor.dispatchEvent( + // "switchToBackupWSForSuperNodesOperations", + // back_ws_url + // ); } } }); @@ -13809,15 +13810,15 @@ Event information log continue; // Get the highest index of connected supernodes - const highestIndexOfConnectedSupernode = Object.keys( - localbitcoinplusplus.backupWS - ).reduce((acc, val) => - acc === undefined || - myClosestSupsFloList.indexOf(val) > - myClosestSupsFloList.indexOf(acc) - ? myClosestSupsFloList.indexOf(val) - : myClosestSupsFloList.indexOf(acc) - ); + let highestIndexOfConnectedSupernode = -1; + + for(let i=0; i<=currenctBackupWsList.length; i++) { + if(myClosestSupsFloList.indexOf(currenctBackupWsList[i]) + >highestIndexOfConnectedSupernode) { + highestIndexOfConnectedSupernode + = myClosestSupsFloList.indexOf(currenctBackupWsList[i]); + } + } if ( myClosestSupsFloList.indexOf(supsObj.trader_flo_address) < @@ -13882,9 +13883,11 @@ Event information log ); if ( - typeof nextClosestSupernodeElem == "object" && - nextClosestSupernodeElem[0].trader_flo_address !== - user_data.myLocalFLOAddress + typeof nextClosestSupernodeElem == "object" + && nextClosestSupernodeElem !== null + && nextClosestSupernodeElem !== undefined + && nextClosestSupernodeElem[0].trader_flo_address + !== user_data.myLocalFLOAddress ) { reactor.dispatchEvent( "shift_ws_connection", @@ -13956,20 +13959,23 @@ Event information log } }); - /*This function is used to verify a tx denoting token transfer by trusted guy to the supernode - and to increase balance of the respective user node.*/ - - reactor.addEventListener( - "request_to_modify_user_balance_on_successfull_fiat_deposit", - function(token_transfer_resp) { - /* - token_transfer_resp = { - tx: ec289d40069bb4db8b633133560269004a35f834c4a146560887ac87f7789d35, - user_action: 'deposit' - } - */ - } - ); + /* This function runs when a backup ws is down */ + reactor.addEventListener("backup_supernode_down", async function(evt) { + showMessage( + `Disconnected to backup Supernode sever: ${evt.srcElement.url}.` + ); + const switchMyWS = new backupSupernodesWebSocketObject(); + switchMyWS.updateSupernodeAvailabilityStatus( + evt.srcElement.url, + false + ); + // Request next backup server to connect + await reactor.dispatchEvent( + "switchToBackupWSForSuperNodesOperations", + evt.srcElement.url + ); + }); + @@ -20295,21 +20301,7 @@ Event information log reactor.dispatchEvent("backup_supernode_up"); }.bind(this); this.ws_connection.onclose = function(evt) { - reactor.addEventListener("backup_supernode_down", async function() { - showMessage( - `Disconnected to backup Supernode sever: ${evt.srcElement.url}.` - ); - switchMyWS.updateSupernodeAvailabilityStatus( - evt.srcElement.url, - false - ); - // Request next backup server to connect - await reactor.dispatchEvent( - "switchToBackupWSForSuperNodesOperations", - evt.srcElement.url - ); - }); - reactor.dispatchEvent("backup_supernode_down"); + reactor.dispatchEvent("backup_supernode_down", evt); }.bind(this); this.ws_connection.onmessage = function(evt) { let response = evt.data; From 0a09c600b83d24b58046979b822aab892fb412f8 Mon Sep 17 00:00:00 2001 From: Abhishek Sinha Date: Thu, 12 Sep 2019 21:28:19 +0530 Subject: [PATCH 2/4] fixed backup ws connection loop issue --- supernode/index.html | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/supernode/index.html b/supernode/index.html index e0a59be..fe913ee 100644 --- a/supernode/index.html +++ b/supernode/index.html @@ -13960,22 +13960,22 @@ Event information log }); /* This function runs when a backup ws is down */ - reactor.addEventListener("backup_supernode_down", async function(evt) { - showMessage( - `Disconnected to backup Supernode sever: ${evt.srcElement.url}.` - ); - const switchMyWS = new backupSupernodesWebSocketObject(); - switchMyWS.updateSupernodeAvailabilityStatus( - evt.srcElement.url, - false - ); - // Request next backup server to connect - await reactor.dispatchEvent( - "switchToBackupWSForSuperNodesOperations", - evt.srcElement.url - ); - }); - + reactor.addEventListener("backup_supernode_down", async function(evt) { + showMessage( + `Disconnected to backup Supernode sever: ${evt.srcElement.url}.` + ); + const switchMyWS = new backupSupernodesWebSocketObject(); + switchMyWS.updateSupernodeAvailabilityStatus( + evt.srcElement.url, + false + ); + // Request next backup server to connect + await reactor.dispatchEvent( + "switchToBackupWSForSuperNodesOperations", + evt.srcElement.url + ); + }); + From a33b5a0c57589540e04c99af58c7a77693a7b00c Mon Sep 17 00:00:00 2001 From: Abhishek Sinha Date: Thu, 12 Sep 2019 21:31:28 +0530 Subject: [PATCH 3/4] fixed backup ws connection loop issue --- supernode/index.html | 6 ------ 1 file changed, 6 deletions(-) diff --git a/supernode/index.html b/supernode/index.html index fe913ee..0c6d56e 100644 --- a/supernode/index.html +++ b/supernode/index.html @@ -13225,10 +13225,8 @@ Event information log reactor.registerEvent("shift_ws_connection"); reactor.registerEvent("switchToBackupWSForSuperNodesOperations"); reactor.registerEvent("user_flo_keys_active"); - reactor.registerEvent("backup_supernode_down"); reactor.registerEvent("remove_extra_backup_connections"); - reactor.addEventListener("fireNodeWelcomeBackEvent", function(evt) { let getFLOId = bitjs.FLO_TEST.pubkey2address(evt.flo_public_key); @@ -13750,10 +13748,6 @@ Event information log } } catch (error) { console.error(error); - // reactor.dispatchEvent( - // "switchToBackupWSForSuperNodesOperations", - // back_ws_url - // ); } } }); From cc0078d3cfa083f05d13d250fa5019f01a570f7c Mon Sep 17 00:00:00 2001 From: Abhishek Sinha Date: Fri, 13 Sep 2019 11:54:56 +0530 Subject: [PATCH 4/4] fixed remove_extra_backup_connections function --- supernode/index.html | 30 +++++------------------------- 1 file changed, 5 insertions(+), 25 deletions(-) diff --git a/supernode/index.html b/supernode/index.html index 0c6d56e..ec1244e 100644 --- a/supernode/index.html +++ b/supernode/index.html @@ -13350,18 +13350,6 @@ Event information log op[0].flo_public_key ) ) { - // Kill the connection manually to ensure connection is really closed. - /* Source of inspiration:- - https://github.com/dart-lang/sdk/issues/25536 - https://bugs.chromium.org/p/chromium/issues/detail?id=76358 - */ - - if ( - localbitcoinplusplus.backupWS[getFLOId].ws_connection - .readyState == 1 - ) { - localbitcoinplusplus.backupWS[getFLOId].ws_connection.close(); - } // Stop yourself from serving it unless proper DB sync localbitcoinplusplus.services[`can_serve_${getFLOId}`] = false; @@ -13902,18 +13890,10 @@ Event information log // remove above lines with these readAllDB(`myClosestSupernodes`).then(sups => { sups - .filter(f => { - const supWSConn = - localbitcoinplusplus.backupWS[f.trader_flo_address]; - if ( - typeof supWSConn == "object" && - supWSConn.ws_connection.readyState < 2 - ) { - return f; - } - }) .map(backup_id => { - if (!localbitcoinplusplus.backupWS.hasOwnProperty(backup_id)) { + if (!localbitcoinplusplus.backupWS.hasOwnProperty(backup_id) + && backup_id.trader_flo_address !== localbitcoinplusplus.wallets.my_local_flo_address + ) { const backup_conns = localbitcoinplusplus.backupWS[backup_id.trader_flo_address]; if (typeof backup_conns.ws_connection == "object") { @@ -13927,13 +13907,13 @@ Event information log localbitcoinplusplus.backupWS[ backup_id.trader_flo_address ].ws_connection.close(); - delete localbitcoinplusplus.backupWS[backup_id]; + delete localbitcoinplusplus.backupWS[backup_id.trader_flo_address]; } } else { localbitcoinplusplus.backupWS[ backup_id.trader_flo_address ].ws_connection.close(); - delete localbitcoinplusplus.backupWS[backup_id]; + delete localbitcoinplusplus.backupWS[backup_id.trader_flo_address]; } } }