fixed errors in backup syncing

This commit is contained in:
Abhishek Sinha 2019-10-15 17:14:55 +05:30
parent 5bc7b4753e
commit c961821a04

View File

@ -13450,8 +13450,8 @@
//if(alowed_supernodes_flo_ids.includes(localbitcoinplusplus.wallets.my_local_flo_address)) { //if(alowed_supernodes_flo_ids.includes(localbitcoinplusplus.wallets.my_local_flo_address)) {
// Get the list of "left side" Supernodes of the disconnected Supernode // Get the list of "left side" Supernodes of the disconnected Supernode
let getLeftSusOfDisconnectedSu = subjectFloIdClosestNodesFloIdsFromLeft.slice(0, my_idx); let getLeftSusOfDisconnectedSu = subjectFloIdClosestNodesFloIdsFromLeft.slice(0, my_idx);
if(getLeftSusOfDisconnectedSu.length<1) return;
console.table(getLeftSusOfDisconnectedSu); console.table(getLeftSusOfDisconnectedSu);
let aNearerLeftSupernodeIsAlive = false; let aNearerLeftSupernodeIsAlive = false;
@ -13736,9 +13736,10 @@
/*Shifting a WS Connection Function*/ /*Shifting a WS Connection Function*/
reactor.addEventListener("shift_ws_connection", function(wsSupsObj) { reactor.addEventListener("shift_ws_connection", function(wsSupsObj) {
console.trace(wsSupsObj);
const getFLOId = wsSupsObj.trader_flo_address; const getFLOId = wsSupsObj.trader_flo_address;
if (getFLOId === localbitcoinplusplus.wallets.my_local_flo_address) if (getFLOId === localbitcoinplusplus.wallets.my_local_flo_address
return; || typeof getFLOId !== "string") return;
const back_ws_url = `ws://${wsSupsObj.ip}:${wsSupsObj.port}`; const back_ws_url = `ws://${wsSupsObj.ip}:${wsSupsObj.port}`;
let currenctBackupWsList = Object.keys( let currenctBackupWsList = Object.keys(
@ -13747,6 +13748,22 @@
m => localbitcoinplusplus.backupWS[m].ws_connection.readyState < 2 m => localbitcoinplusplus.backupWS[m].ws_connection.readyState < 2
); );
const reqFloIdIndex = Object.keys(localbitcoinplusplus.myClosestSupernodes).indexOf(getFLOId);
// Get the highest index of connected supernodes
let highestIndexOfConnectedSupernode = -1;
for(let i=0; i<=currenctBackupWsList.length; i++) {
if(localbitcoinplusplus.myClosestSupernodes.indexOf(currenctBackupWsList[i])
>highestIndexOfConnectedSupernode) {
highestIndexOfConnectedSupernode
= localbitcoinplusplus.myClosestSupernodes.indexOf(currenctBackupWsList[i]);
}
}
// Only if a nearer Supernode than any connected Supernode can pass through
if(reqFloIdIndex>=highestIndexOfConnectedSupernode) return;
if ( if (
typeof localbitcoinplusplus.backupWS[getFLOId] === "object" && typeof localbitcoinplusplus.backupWS[getFLOId] === "object" &&
localbitcoinplusplus.backupWS[getFLOId].ws_connection.readyState == 1 localbitcoinplusplus.backupWS[getFLOId].ws_connection.readyState == 1
@ -13846,8 +13863,8 @@
highestIndexOfConnectedSupernode highestIndexOfConnectedSupernode
) { ) {
/* Break the loop as soon as this line runs /* Break the loop as soon as this line runs
as any further ws conn releated event will be as any further ws conn releated event will be
taken care in shift_ws_connection event itself. */ taken care in shift_ws_connection event itself. */
reactor.dispatchEvent("shift_ws_connection", supsObj); reactor.dispatchEvent("shift_ws_connection", supsObj);
break; break;
} else { } else {
@ -13890,27 +13907,21 @@
`myClosestSupernodes` `myClosestSupernodes`
); );
let nextClosestSupernodeElem = myClosestSupernodesArray.filter( const switchMyWS = new backupSupernodesWebSocketObject();
(wew, index) => { let dis_floid = await switchMyWS.getFloIdFromWSUrl(disconnected_url);
let ww = `ws://${wew.ip}:${wew.port}`; let nextClosestSupernodeElem = await getNextSupernode(dis_floid);
if (typeof z == "boolean" && z) { console.log("nextClosestSupernodeElem", nextClosestSupernodeElem);
z = false;
return ww;
}
if (ww == disconnected_url) z = true;
}
);
if ( if (
typeof nextClosestSupernodeElem == "object" typeof nextClosestSupernodeElem == "object"
&& nextClosestSupernodeElem !== null && nextClosestSupernodeElem !== null
&& nextClosestSupernodeElem !== undefined && nextClosestSupernodeElem !== undefined
&& nextClosestSupernodeElem[0].trader_flo_address && nextClosestSupernodeElem.trader_flo_address
!== user_data.myLocalFLOAddress !== user_data.myLocalFLOAddress
) { ) {
reactor.dispatchEvent( reactor.dispatchEvent(
"shift_ws_connection", "shift_ws_connection",
nextClosestSupernodeElem[0] nextClosestSupernodeElem
); );
} else { } else {
console.log(nextClosestSupernodeElem); console.log(nextClosestSupernodeElem);
@ -13958,29 +13969,37 @@
}); });
/*This function denotes that a user is probably live and is using the system*/ /*This function denotes that a user is probably live and is using the system*/
reactor.addEventListener("user_flo_keys_active", async function(usrObj) { reactor.addEventListener("user_flo_keys_active", async function(FLOPublicKey="") {
if ( try {
localbitcoinplusplus.master_configurations.supernodesPubKeys.includes( if(typeof localbitcoinplusplus.wallets.MY_SUPERNODE_PRIVATE_KEY!=="string")
usrObj.myLocalFLOPublicKey throw new Error('Private key is not set.');
)
) {
// Connect to nearest live backup nodes
reactor.dispatchEvent("resolve_backup_ws_connections");
// Inform left side Supernodes you are back
const RM_WALLET = new localbitcoinplusplus.wallets; const RM_WALLET = new localbitcoinplusplus.wallets;
const msg_obj = {}; const userKeysObj = RM_WALLET.generateFloKeys(localbitcoinplusplus.wallets.MY_SUPERNODE_PRIVATE_KEY);
msg_obj.protocol = '__ALL_SUPERNODES_MSG__'; if (
msg_obj.event = 'supernode_came_back'; typeof userKeysObj==="object"
msg_obj.data = { subject_flo_id: localbitcoinplusplus.wallets.my_local_flo_address }; && localbitcoinplusplus.master_configurations.supernodesPubKeys.includes(
msg_obj.initialSender = localbitcoinplusplus.wallets.my_local_flo_address; userKeysObj.pubKeyHex
msg_obj.su_pubKey = localbitcoinplusplus.wallets.my_local_flo_public_key; )
msg_obj.hash = Crypto.SHA256(msg_obj); ) {
msg_obj.sign = RM_WALLET.sign( // Connect to nearest live backup nodes
msg_obj.hash, reactor.dispatchEvent("resolve_backup_ws_connections");
localbitcoinplusplus.wallets.MY_SUPERNODE_PRIVATE_KEY // Inform left side Supernodes you are back
); const msg_obj = {};
reactor.dispatchEvent("informLeftSuperNode", msg_obj, msg_obj.protocol = '__ALL_SUPERNODES_MSG__';
n = localbitcoinplusplus.master_configurations.MaxBackups); msg_obj.event = 'supernode_came_back';
msg_obj.data = { subject_flo_id: userKeysObj.address };
msg_obj.initialSender = userKeysObj.address;
msg_obj.su_pubKey = userKeysObj.pubKeyHex;
msg_obj.hash = Crypto.SHA256(msg_obj);
msg_obj.sign = RM_WALLET.sign(
msg_obj.hash,
localbitcoinplusplus.wallets.MY_SUPERNODE_PRIVATE_KEY
);
reactor.dispatchEvent("informLeftSuperNode", msg_obj,
n = localbitcoinplusplus.master_configurations.MaxBackups);
}
} catch (error) {
throw new Error(error);
} }
}); });
@ -13989,16 +14008,18 @@
showMessage(`Disconnected to backup Supernode sever: ${evt.srcElement.url}.`); showMessage(`Disconnected to backup Supernode sever: ${evt.srcElement.url}.`);
const switchMyWS = new backupSupernodesWebSocketObject(); const switchMyWS = new backupSupernodesWebSocketObject();
const disconnected_su_flo_id = await switchMyWS.getFloIdFromWSUrl(evt.srcElement.url); const disconnected_su_flo_id = await switchMyWS.getFloIdFromWSUrl(evt.srcElement.url);
// switchMyWS.updateSupernodeAvailabilityStatus( switchMyWS.updateSupernodeAvailabilityStatus(
// evt.srcElement.url, evt.srcElement.url,
// false false
// );
reactor.dispatchEvent("fireNodeGoodByeEvent", disconnected_su_flo_id);
// Request next backup server to connect
await reactor.dispatchEvent(
"switchToBackupWSForSuperNodesOperations",
evt.srcElement.url
); );
if(!evt.wasClean) {
reactor.dispatchEvent("fireNodeGoodByeEvent", disconnected_su_flo_id);
// Request next backup server to connect
await reactor.dispatchEvent(
"switchToBackupWSForSuperNodesOperations",
evt.srcElement.url
);
}
}); });
reactor.addEventListener("backup_supernode_up", async function(url='') { reactor.addEventListener("backup_supernode_up", async function(url='') {
@ -14166,7 +14187,7 @@
allSus = allSus.filter((f,i)=>i<idx); allSus = allSus.filter((f,i)=>i<idx);
} }
} }
for (let i = allSus.length; i >= 0; i--) { for (let i = allSus.length-1; i >= 0; i--) {
let prevSuObj = allSus[i]; let prevSuObj = allSus[i];
let nextSu = prevSuObj.trader_flo_address; let nextSu = prevSuObj.trader_flo_address;
if (nextSu===flo_id) continue; if (nextSu===flo_id) continue;
@ -14272,15 +14293,17 @@
}); });
reactor.addEventListener("informLeftSuperNode", async function(msg_obj={}, n=0) { reactor.addEventListener("informLeftSuperNode", async function(msg_obj={}, n=0) {
const pubkey = localbitcoinplusplus.wallets.my_local_flo_public_key|| msg_obj.su_pubKey;
const usrAddr = localbitcoinplusplus.wallets.my_local_flo_address || bitjs.FLO_TEST.pubkey2address(pubkey);
if (localbitcoinplusplus.master_configurations.supernodesPubKeys if (localbitcoinplusplus.master_configurations.supernodesPubKeys
.includes(localbitcoinplusplus.wallets.my_local_flo_public_key)) { .includes(pubkey)) {
let m = localbitcoinplusplus.master_configurations.MaxBackups; let m = localbitcoinplusplus.master_configurations.MaxBackups;
let successfully_informed=0; let successfully_informed=0;
if(n>0) m=n; if(n>0) m=n;
let suList = await getPrevSupernode(); let suList = await getPrevSupernode();
let sn = suList.trader_flo_address; let sn = suList.trader_flo_address;
if(sn===msg_obj.data.subject_flo_id || sn===localbitcoinplusplus.wallets.my_local_flo_address) { if(sn===msg_obj.data.subject_flo_id || sn===usrAddr) {
suList = await getPrevSupernode(sn); suList = await getPrevSupernode(sn);
sn = suList.trader_flo_address; sn = suList.trader_flo_address;
} }
@ -14292,7 +14315,7 @@
tempWSL = {}; tempWSL = {};
//for(let sn in localbitcoinplusplus.myClosestSupernodes) { //for(let sn in localbitcoinplusplus.myClosestSupernodes) {
(function informOneSupernode(sn) { (function informOneSupernode(sn) {
if(sn!==localbitcoinplusplus.wallets.my_local_flo_address if(sn!==usrAddr
&& sn!==msg_obj.data.subject_flo_id && sn!==msg_obj.data.subject_flo_id
&& websocket.readyState===1) { && websocket.readyState===1) {
const switchMyWS = new backupSupernodesWebSocketObject(); const switchMyWS = new backupSupernodesWebSocketObject();
@ -14302,30 +14325,30 @@
tempWSL[sn].onopen = async function(evt) { tempWSL[sn].onopen = async function(evt) {
await localbitcoinplusplus.actions.delay(5000); await localbitcoinplusplus.actions.delay(5000);
tempWSL[sn].send(JSON.stringify(msg_obj)); tempWSL[sn].send(JSON.stringify(msg_obj));
successfully_informed++;
console.info("successfully_informed:", successfully_informed);
await localbitcoinplusplus.actions.delay(5000); await localbitcoinplusplus.actions.delay(5000);
tempWSL[sn].close(); tempWSL[sn].close();
if(successfully_informed<m) { if(successfully_informed<m) {
let openedFloId = await switchMyWS.getFloIdFromWSUrl(evt.srcElement.url); let openedFloId = await switchMyWS.getFloIdFromWSUrl(evt.srcElement.url);
let nxtSup = await getPrevSupernode(openedFloId); let nxtSup = await getPrevSupernode(openedFloId);
if(typeof nxtSup !== "string") return; if(typeof nxtSup.trader_flo_address !== "string") return;
msg_obj.receiverFloAddress = nxtSup; msg_obj.receiverFloAddress = nxtSup.trader_flo_address;
informOneSupernode(nxtSup.trader_flo_address); informOneSupernode(nxtSup.trader_flo_address);
} else return; } else return;
successfully_informed++;
console.info("successfully_informed:", successfully_informed);
}; };
tempWSL[sn].onclose = async function(evt) { tempWSL[sn].onclose = async function(evt) {
console.info(`Closed ${evt.srcElement.url}`); console.info(`Closed ${evt.srcElement.url}`);
if(!evt.wasClean) { if(!evt.wasClean) {
let closedFloId = await switchMyWS.getFloIdFromWSUrl(evt.srcElement.url); let closedFloId = await switchMyWS.getFloIdFromWSUrl(evt.srcElement.url);
let nxtSu = await getPrevSupernode(closedFloId); let nxtSu = await getPrevSupernode(closedFloId);
if(typeof nxtSu !== "string") return; if(typeof nxtSu.trader_flo_address !== "string") return;
msg_obj.receiverFloAddress = nxtSu; msg_obj.receiverFloAddress = nxtSu.trader_flo_address;
if ((nxtSu.trader_flo_address==localbitcoinplusplus.wallets.my_local_flo_address if ((nxtSu.trader_flo_address==usrAddr
|| nxtSu.trader_flo_address==msg_obj.data.subject_flo_id)) { || nxtSu.trader_flo_address==msg_obj.data.subject_flo_id)) {
let nxtSu2 = await getPrevSupernode(nxtSu.trader_flo_address); let nxtSu2 = await getPrevSupernode(nxtSu.trader_flo_address);
if(typeof nxtSu2 !== "string") return; if(typeof nxtSu2.trader_flo_address !== "string") return;
msg_obj.receiverFloAddress = nxtSu2; msg_obj.receiverFloAddress = nxtSu2.trader_flo_address;
informOneSupernode(nxtSu2.trader_flo_address); informOneSupernode(nxtSu2.trader_flo_address);
} else if(typeof nxtSu !== "undefined") { } else if(typeof nxtSu !== "undefined") {
informOneSupernode(nxtSu.trader_flo_address); informOneSupernode(nxtSu.trader_flo_address);
@ -14668,6 +14691,16 @@
dataBaseUIOperations(); dataBaseUIOperations();
} }
// Private key is built. Now execute private key dependent functions
const RM_WALLET = new localbitcoinplusplus.wallets;
const pubkey = RM_WALLET
.generateFloKeys(localbitcoinplusplus.wallets.MY_SUPERNODE_PRIVATE_KEY)
.pubKeyHex
if(localbitcoinplusplus.master_configurations.supernodesPubKeys
.includes(pubkey)) {
reactor.dispatchEvent("user_flo_keys_active", pubkey);
}
Promise.resolve(true); Promise.resolve(true);
} else { } else {
let mes = `WARNING: Failed to build your private key. You can reset keys and generate new keys from keys section below.`; let mes = `WARNING: Failed to build your private key. You can reset keys and generate new keys from keys section below.`;
@ -14702,6 +14735,15 @@
enumerable: false enumerable: false
} }
); );
// Private key is built. Now execute private key dependent functions
const pubkey = RM_WALLET
.generateFloKeys(localbitcoinplusplus.wallets.MY_SUPERNODE_PRIVATE_KEY)
.pubKeyHex
if(localbitcoinplusplus.master_configurations.supernodesPubKeys
.includes(pubkey)) {
reactor.dispatchEvent("user_flo_keys_active", pubkey);
}
}, },
rebuild_private_key: function( rebuild_private_key: function(
private_key_shamirs_shares, private_key_shamirs_shares,
@ -20456,8 +20498,6 @@
idbData.myLocalFLOPublicKey idbData.myLocalFLOPublicKey
) )
) { ) {
// Invoke functions
reactor.dispatchEvent("user_flo_keys_active", idbData);
wsUri.map((uri, index) => { wsUri.map((uri, index) => {
// Do not serve to any requests unless data is fully synced. // Do not serve to any requests unless data is fully synced.
@ -20582,8 +20622,6 @@
idbData.myLocalFLOPublicKey idbData.myLocalFLOPublicKey
) )
) { ) {
// Invoke functions
reactor.dispatchEvent("user_flo_keys_active", idbData);
wsUri.map((uri, index) => { wsUri.map((uri, index) => {
// Do not serve to any requests unless data is fully synced. // Do not serve to any requests unless data is fully synced.
@ -20891,7 +20929,6 @@
} }
function onClose(evt) { function onClose(evt) {
reactor.dispatchEvent("primary_supernode_down", evt);
// Also close the backup ws conns // Also close the backup ws conns
if(localbitcoinplusplus.master_configurations.supernodesPubKeys if(localbitcoinplusplus.master_configurations.supernodesPubKeys
.includes(localbitcoinplusplus.wallets.my_local_flo_public_key)) { .includes(localbitcoinplusplus.wallets.my_local_flo_public_key)) {
@ -20899,6 +20936,7 @@
localbitcoinplusplus.backupWS[conn].ws_connection.close(); localbitcoinplusplus.backupWS[conn].ws_connection.close();
} }
} }
reactor.dispatchEvent("primary_supernode_down", evt);
} }
async function onMessage(evt) { async function onMessage(evt) {