Skip to content
This repository has been archived by the owner on Sep 5, 2020. It is now read-only.

Commit

Permalink
improved nodeSync
Browse files Browse the repository at this point in the history
  • Loading branch information
frozeman committed Dec 3, 2015
1 parent 1b764b7 commit 3e8b9dc
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 116 deletions.
3 changes: 2 additions & 1 deletion interface/client/templates/popupWindows/splashScreen.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 += '<br><small>'+ TAPi18n.__('mist.startScreen.nodeSyncProcessing') +'</small>';
else
translatedText += '<br><small>'+ TAPi18n.__('mist.startScreen.nodeSyncInfo', lastData) +'</small>';
Expand Down
5 changes: 4 additions & 1 deletion main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
202 changes: 91 additions & 111 deletions modules/checkNodeSync.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
};
};
3 changes: 2 additions & 1 deletion modules/ipc/nodeConnector.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
3 changes: 1 addition & 2 deletions modules/menuItems.js
Original file line number Diff line number Diff line change
Expand Up @@ -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){
Expand Down

0 comments on commit 3e8b9dc

Please sign in to comment.