From 3e8b9dc7ec3b641eb6cff02d43acd82f9a4b0d9c Mon Sep 17 00:00:00 2001 From: Fabian Vogelsteller Date: Thu, 3 Dec 2015 20:07:27 +0100 Subject: [PATCH] improved nodeSync --- .../templates/popupWindows/splashScreen.js | 3 +- main.js | 5 +- modules/checkNodeSync.js | 202 ++++++++---------- modules/ipc/nodeConnector.js | 3 +- modules/menuItems.js | 3 +- 5 files changed, 100 insertions(+), 116 deletions(-) diff --git a/interface/client/templates/popupWindows/splashScreen.js b/interface/client/templates/popupWindows/splashScreen.js index af8441587..b3fab2c8a 100644 --- a/interface/client/templates/popupWindows/splashScreen.js +++ b/interface/client/templates/popupWindows/splashScreen.js @@ -65,7 +65,8 @@ Template['popupWindows_splashScreen'].onCreated(function(){ } else { // show progress bar TemplateVar.set(template, 'showProgressBar', true); - if(lastData._highestBlock - lastData._currentBlock < 5000) + + if(lastData._highestBlock - lastData._currentBlock < 2500) translatedText += '
'+ TAPi18n.__('mist.startScreen.nodeSyncProcessing') +''; else translatedText += '
'+ TAPi18n.__('mist.startScreen.nodeSyncInfo', lastData) +''; diff --git a/main.js b/main.js index cf8b412b9..92e0582ae 100644 --- a/main.js +++ b/main.js @@ -373,7 +373,10 @@ app.on('ready', function() { clearInterval(intervalId); - checkNodeSync(socket, appStartWindow, function(e){ + // update menu, to show node switching possibilities + appMenu([]); + + checkNodeSync(appStartWindow, function(e){ appStartWindow.webContents.send('startScreenText', 'mist.startScreen.startedNode'); clearSocket(socket); diff --git a/modules/checkNodeSync.js b/modules/checkNodeSync.js index e23560630..676320961 100644 --- a/modules/checkNodeSync.js +++ b/modules/checkNodeSync.js @@ -8,151 +8,131 @@ checks the current node whether its synching or not and how much it kept up alre const _ = require('underscore'); const ipc = require('electron').ipcMain; -var dechunker = require('./ipc/dechunker.js'); +// var dechunker = require('./ipc/dechunker.js'); /** Check if the nodes needs sync and start the app @method checkNodeSync */ -module.exports = function(socket, appStartWindow, callback){ - var idCount = 1, - getLatestBlock = { - jsonrpc: '2.0', - id: idCount, - method: 'eth_getBlockByNumber', - params: ['latest', false] - }, - intervalId, +module.exports = function(appStartWindow, callback){ + var intervalId, timeoutId, cbCalled = false; console.log('Checking node sync status...'); - socket.on("data", function(data){ - dechunker(data, function(error, result){ - - if(error) { - console.error('Error: couldn\'t decode node response!', error); - callback(error); - return; - } + global.nodeConnector.connect(); + + // check last block time + global.nodeConnector.send('eth_getBlockByNumber', ['latest', false], function(e, result){ + var now = Math.floor(new Date().getTime() / 1000); + + console.log('Time between last block', (now - +result.timestamp) + 's'); - // error occured, ignore - if(result.error) { - // if sync method is not implemented, just start the app - if(result.error.code === -32601) { - console.log('Syncing method not implemented, start app anyway.'); + // need sync if > 2 minutes + if(now - +result.timestamp > 60 * 2) { - clearInterval(intervalId); - clearTimeout(timeoutId); - callback(); - cbCalled = true; + intervalId = setInterval(function(){ + + // get the latest sync status + if(global.nodeConnector.socket.writable) { + global.nodeConnector.send('eth_syncing', [], cb); } + }, 50); - return; - } + // start app + } else { + console.log('No sync necessary, starting app!'); + callback(); + cbCalled = true; + } + }); - // FIRST BLOCK arrived - if(result.id === 1) { - var now = Math.floor(new Date().getTime() / 1000); - console.log('Time between last block', (now - +result.result.timestamp) + 's'); + var cb = function(error, result){ + + // error occured, ignore + if(result.error) { + // if sync method is not implemented, just start the app + if(result.error.code === -32601) { + console.log('Syncing method not implemented, start app anyway.'); - // need sync if > 2 minutes - if(now - +result.result.timestamp > 60 * 2) { + clearInterval(intervalId); + clearTimeout(timeoutId); + callback(); + cbCalled = true; + } - intervalId = setInterval(function(){ - idCount++; + return; + } + - // get the latest sync status - if(socket.writable) { - socket.write(JSON.stringify({ - jsonrpc: '2.0', - id: idCount, - method: 'eth_syncing', - params: [] - })); - } - }, 50); + // CHECK BLOCK (AGAIN) + if(_.isString(result.hash)) { + var now = Math.floor(new Date().getTime() / 1000); + // If ready! + if(now - +result.timestamp < 120 && !cbCalled) { + console.log('Sync finished, starting app!'); - // start app - } else { - console.log('No sync necessary, starting app!'); - callback(); - cbCalled = true; - } + clearInterval(intervalId); + clearTimeout(timeoutId); + callback(); - // CHECK BLOCK (AGAIN) - } else if(_.isString(result.result.hash)) { - var now = Math.floor(new Date().getTime() / 1000); + // prevent double call of the callback + cbCalled = true; - // If ready! - if(now - +result.result.timestamp < 120 && !cbCalled) { - console.log('Sync finished, starting app!'); + // if still needs syncing + } else { + if(appStartWindow && appStartWindow.webContents) + appStartWindow.webContents.send('startScreenText', 'mist.startScreen.nodeSyncing', { + currentBlock: +result.number + }); + } - clearInterval(intervalId); - clearTimeout(timeoutId); - callback(); - // prevent double call of the callback - cbCalled = true; + // CHECK SYNC STATUS + } else { + + // if not syncing anymore + if(!result) { - // if still needs syncing - } else { - if(appStartWindow && appStartWindow.webContents) - appStartWindow.webContents.send('startScreenText', 'mist.startScreen.nodeSyncing', { - currentBlock: +result.result.number - }); - } + global.nodeConnector.send('eth_getBlockByNumber', ['latest', false], cb); + + // create timeout for private chains, where no one is mining + if(!timeoutId) { + timeoutId = setTimeout(function(){ + if(appStartWindow && appStartWindow.webContents) { + appStartWindow.webContents.send('startScreenText', 'mist.startScreen.privateChainTimeout'); + ipc.on('uiAction_startApp', function() { + clearInterval(intervalId); + callback(); - // CHECK SYNC STATUS + // prevent double call of the callback + cbCalled = true; + }); + } + }, 1000 * 12); + } + + // update progress bar } else { + + // clear timeout if blocks start to get imported + clearTimeout(timeoutId); + timeoutId = null; - // if not syncing anymore - if(!result.result) { - getLatestBlock.id = ++idCount; - socket.write(JSON.stringify(getLatestBlock)); - - // create timeout for private chains, where no one is mining - if(!timeoutId) { - timeoutId = setTimeout(function(){ - if(appStartWindow && appStartWindow.webContents) { - appStartWindow.webContents.send('startScreenText', 'mist.startScreen.privateChainTimeout'); - - ipc.on('uiAction_startApp', function() { - clearInterval(intervalId); - callback(); - - // prevent double call of the callback - cbCalled = true; - }); - } - }, 1000 * 12); - } - - // update progress bar - } else { - - // clear timeout if blocks start to get imported - clearTimeout(timeoutId); - timeoutId = null; - - // remove the private chain button again - appStartWindow.webContents.send('startScreenText', 'mist.startScreen.privateChainTimeoutClear'); - - if(appStartWindow && appStartWindow.webContents) - appStartWindow.webContents.send('startScreenText', 'mist.startScreen.nodeSyncing', result.result); - } + // remove the private chain button again + appStartWindow.webContents.send('startScreenText', 'mist.startScreen.privateChainTimeoutClear'); + if(appStartWindow && appStartWindow.webContents) + appStartWindow.webContents.send('startScreenText', 'mist.startScreen.nodeSyncing', result); } - }); - }); - // get the latest block and compare timestamp - socket.write(JSON.stringify(getLatestBlock)); + } + }; }; \ No newline at end of file diff --git a/modules/ipc/nodeConnector.js b/modules/ipc/nodeConnector.js index 9831c0fd3..58ffd1902 100644 --- a/modules/ipc/nodeConnector.js +++ b/modules/ipc/nodeConnector.js @@ -46,7 +46,8 @@ var NodeConnector = function(ipcPath) { }; NodeConnector.prototype.connect = function() { - this.socket.connect({path: this.ipcPath}); + if(!this.socket.writable) + this.socket.connect({path: this.ipcPath}); }; NodeConnector.prototype.send = function(name, params, callback) { diff --git a/modules/menuItems.js b/modules/menuItems.js index 461e2e71d..dc1249249 100644 --- a/modules/menuItems.js +++ b/modules/menuItems.js @@ -241,8 +241,7 @@ var menuTempl = function(webviews) { enabled: (global.network === 'test'), click: function(){ // TODO remove on new RPC - if(!global.nodeConnector.socket.writable) - global.nodeConnector.connect(); + global.nodeConnector.connect(); if(!global.mining) { global.nodeConnector.send('miner_start', [1], function(e, result){