Merge pull request #10 from avishkarabhishek786/master_bug_ws_connection_loop

fixed bug ws connection loop and in remove_extra_backup_connections function
This commit is contained in:
Abhishek Sinha 2019-09-13 12:10:54 +05:30 committed by GitHub
commit 9e7efd4cbf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -13226,10 +13226,7 @@ Event information log
reactor.registerEvent("switchToBackupWSForSuperNodesOperations"); reactor.registerEvent("switchToBackupWSForSuperNodesOperations");
reactor.registerEvent("user_flo_keys_active"); reactor.registerEvent("user_flo_keys_active");
reactor.registerEvent("remove_extra_backup_connections"); reactor.registerEvent("remove_extra_backup_connections");
reactor.registerEvent(
"request_to_modify_user_balance_on_successfull_fiat_deposit"
);
reactor.addEventListener("fireNodeWelcomeBackEvent", function(evt) { reactor.addEventListener("fireNodeWelcomeBackEvent", function(evt) {
let getFLOId = bitjs.FLO_TEST.pubkey2address(evt.flo_public_key); let getFLOId = bitjs.FLO_TEST.pubkey2address(evt.flo_public_key);
@ -13353,18 +13350,6 @@ Event information log
op[0].flo_public_key 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 // Stop yourself from serving it unless proper DB sync
localbitcoinplusplus.services[`can_serve_${getFLOId}`] = false; localbitcoinplusplus.services[`can_serve_${getFLOId}`] = false;
@ -13738,21 +13723,19 @@ Event information log
localbitcoinplusplus.backupWS[getFLOId].ws_connection localbitcoinplusplus.backupWS[getFLOId].ws_connection
.readyState >= 2 .readyState >= 2
) { ) {
localbitcoinplusplus.backupWS[getFLOId] = null; if (getFLOId !== localbitcoinplusplus.wallets.my_local_flo_address) {
localbitcoinplusplus.backupWS[ localbitcoinplusplus.backupWS[getFLOId] = null;
getFLOId localbitcoinplusplus.backupWS[
] = new backupSupernodesWebSocketObject(back_ws_url); getFLOId
localbitcoinplusplus.backupWS[getFLOId].connectWS(); ] = new backupSupernodesWebSocketObject(back_ws_url);
/* Now in connectWS if connection fails call switchToBackupWSForSuperNodesOperations() in onClose() 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 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) { } catch (error) {
console.error(error); console.error(error);
reactor.dispatchEvent(
"switchToBackupWSForSuperNodesOperations",
back_ws_url
);
} }
} }
}); });
@ -13809,15 +13792,15 @@ Event information log
continue; continue;
// Get the highest index of connected supernodes // Get the highest index of connected supernodes
const highestIndexOfConnectedSupernode = Object.keys( let highestIndexOfConnectedSupernode = -1;
localbitcoinplusplus.backupWS
).reduce((acc, val) => for(let i=0; i<=currenctBackupWsList.length; i++) {
acc === undefined || if(myClosestSupsFloList.indexOf(currenctBackupWsList[i])
myClosestSupsFloList.indexOf(val) > >highestIndexOfConnectedSupernode) {
myClosestSupsFloList.indexOf(acc) highestIndexOfConnectedSupernode
? myClosestSupsFloList.indexOf(val) = myClosestSupsFloList.indexOf(currenctBackupWsList[i]);
: myClosestSupsFloList.indexOf(acc) }
); }
if ( if (
myClosestSupsFloList.indexOf(supsObj.trader_flo_address) < myClosestSupsFloList.indexOf(supsObj.trader_flo_address) <
@ -13882,9 +13865,11 @@ Event information log
); );
if ( if (
typeof nextClosestSupernodeElem == "object" && typeof nextClosestSupernodeElem == "object"
nextClosestSupernodeElem[0].trader_flo_address !== && nextClosestSupernodeElem !== null
user_data.myLocalFLOAddress && nextClosestSupernodeElem !== undefined
&& nextClosestSupernodeElem[0].trader_flo_address
!== user_data.myLocalFLOAddress
) { ) {
reactor.dispatchEvent( reactor.dispatchEvent(
"shift_ws_connection", "shift_ws_connection",
@ -13905,18 +13890,10 @@ Event information log
// remove above lines with these // remove above lines with these
readAllDB(`myClosestSupernodes`).then(sups => { readAllDB(`myClosestSupernodes`).then(sups => {
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 => { .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 = const backup_conns =
localbitcoinplusplus.backupWS[backup_id.trader_flo_address]; localbitcoinplusplus.backupWS[backup_id.trader_flo_address];
if (typeof backup_conns.ws_connection == "object") { if (typeof backup_conns.ws_connection == "object") {
@ -13930,13 +13907,13 @@ Event information log
localbitcoinplusplus.backupWS[ localbitcoinplusplus.backupWS[
backup_id.trader_flo_address backup_id.trader_flo_address
].ws_connection.close(); ].ws_connection.close();
delete localbitcoinplusplus.backupWS[backup_id]; delete localbitcoinplusplus.backupWS[backup_id.trader_flo_address];
} }
} else { } else {
localbitcoinplusplus.backupWS[ localbitcoinplusplus.backupWS[
backup_id.trader_flo_address backup_id.trader_flo_address
].ws_connection.close(); ].ws_connection.close();
delete localbitcoinplusplus.backupWS[backup_id]; delete localbitcoinplusplus.backupWS[backup_id.trader_flo_address];
} }
} }
} }
@ -13956,20 +13933,23 @@ Event information log
} }
}); });
/*This function is used to verify a tx denoting token transfer by trusted guy to the supernode /* This function runs when a backup ws is down */
and to increase balance of the respective user node.*/ reactor.addEventListener("backup_supernode_down", async function(evt) {
showMessage(
reactor.addEventListener( `Disconnected to backup Supernode sever: ${evt.srcElement.url}.`
"request_to_modify_user_balance_on_successfull_fiat_deposit", );
function(token_transfer_resp) { const switchMyWS = new backupSupernodesWebSocketObject();
/* switchMyWS.updateSupernodeAvailabilityStatus(
token_transfer_resp = { evt.srcElement.url,
tx: ec289d40069bb4db8b633133560269004a35f834c4a146560887ac87f7789d35, false
user_action: 'deposit' );
} // Request next backup server to connect
*/ await reactor.dispatchEvent(
} "switchToBackupWSForSuperNodesOperations",
); evt.srcElement.url
);
});
</script> </script>
<!-- Misc functions --> <!-- Misc functions -->
@ -20295,21 +20275,7 @@ Event information log
reactor.dispatchEvent("backup_supernode_up"); reactor.dispatchEvent("backup_supernode_up");
}.bind(this); }.bind(this);
this.ws_connection.onclose = function(evt) { this.ws_connection.onclose = function(evt) {
reactor.addEventListener("backup_supernode_down", async function() { reactor.dispatchEvent("backup_supernode_down", evt);
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");
}.bind(this); }.bind(this);
this.ws_connection.onmessage = function(evt) { this.ws_connection.onmessage = function(evt) {
let response = evt.data; let response = evt.data;