Skip to content
Snippets Groups Projects
Commit f44b2af7 authored by Cédric Moreau's avatar Cédric Moreau
Browse files

Fixing #450 Add a synchronization bar on the UI while pulling blocks from the network

parent 666cb5cb
No related branches found
No related tags found
No related merge requests found
......@@ -30,6 +30,12 @@ function WebAdmin (dbConf, overConf) {
let bmapi;
const that = this;
server.pipe(es.mapSync(function(data) {
if (data.pulling !== undefined) {
that.push(data);
}
}));
AbstractController.call(this, server);
stream.Duplex.call(this, { objectMode: true });
......
......@@ -118,6 +118,12 @@ module.exports = function(dbConf, overConf, interfaces, httpLogs) {
value: data.stopped
}));
}
if (data.pulling !== undefined) {
wssEvents.broadcast(JSON.stringify({
type: 'pulling',
value: data.pulling
}));
}
}));
});
......
......@@ -489,10 +489,20 @@ function PeeringService(server) {
}
});
function pullingEvent(type, number) {
server.push({
pulling: {
type: type,
data: number
}
});
}
function syncBlock(callback, pubkey) {
currentSyncP = co(function *() {
let current = yield dal.getCurrentBlockOrNull();
if (current) {
pullingEvent('start', current.number);
logger.info("Pulling blocks from the network...");
let peers = yield dal.findAllPeersNEWUPBut([selfPubkey]);
peers = _.shuffle(peers);
......@@ -501,11 +511,13 @@ function PeeringService(server) {
}
for (let i = 0, len = peers.length; i < len; i++) {
let p = new Peer(peers[i]);
pullingEvent('peer', _.extend({ number: i, length: peers.length }, p));
logger.trace("Try with %s %s", p.getURL(), p.pubkey.substr(0, 6));
try {
let node = yield p.connect();
node.pubkey = p.pubkey;
yield checkPeerValidity(p, node);
let lastDownloaded;
let dao = pulling.abstractDao({
// Get the local blockchain current block
......@@ -537,6 +549,11 @@ function PeeringService(server) {
// Simulate the adding of a single new block on local blockchain
applyMainBranch: (block) => co(function *() {
let addedBlock = yield server.BlockchainService.submitBlock(block, true, constants.FORK_ALLOWED);
if (!lastDownloaded) {
lastDownloaded = yield dao.remoteCurrent(node);
}
pullingEvent('applying', { number: block.number, last: lastDownloaded.number });
current = addedBlock;
server.streamPush(addedBlock);
}),
......@@ -550,6 +567,15 @@ function PeeringService(server) {
}),
// Simulates the downloading of blocks from a peer
downloadBlocks: (thePeer, fromNumber, count) => co(function*() {
if (!count) {
count = CONST_BLOCKS_CHUNK;
}
pullingEvent('downloading', count);
const blocks = yield Q.nfcall(thePeer.blockchain.blocks, count, fromNumber);
lastDownloaded = blocks[blocks.length - 1];
return blocks;
})
downloadBlocks: (thePeer, fromNumber, count) => co(function*() {
if (!count) {
count = CONST_BLOCKS_CHUNK;
......@@ -577,11 +603,13 @@ function PeeringService(server) {
}
}
}
pullingEvent('end', current.number);
}
logger.info('Will pull blocks from the network in %s min %s sec', Math.floor(SYNC_BLOCK_INTERVAL / 60), Math.floor(SYNC_BLOCK_INTERVAL % 60));
callback && callback();
})
.catch((err) => {
pullingEvent('error');
logger.warn(err.code || err.stack || err.message || err);
callback && callback();
});
......
Subproject commit 084b5dc344e57a673c1f1dd9bd8e5384365b9386
Subproject commit 113f9176e8b2281ab75ba02f854521b099a5e8c8
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment