diff --git a/app/service/PeeringService.js b/app/service/PeeringService.js index ff7351617cd9b15cb4bb2221a9d81d79656965d2..1c8b2be101350647e5ad976a38182c74cd3b1f1a 100644 --- a/app/service/PeeringService.js +++ b/app/service/PeeringService.js @@ -52,7 +52,7 @@ function PeeringService(server, pair, dal) { this.submitP = function(peering, eraseIfAlreadyRecorded, cautious){ let thePeer = new Peer(peering); let sp = thePeer.block.split('-'); - let blockNumber = sp[0]; + let blockNumber = parseInt(sp[0]); let blockHash = sp[1]; let sigTime = 0; let block; @@ -84,7 +84,7 @@ function PeeringService(server, pair, dal) { if(found){ // Already existing peer var sp2 = found.block.split('-'); - var previousBlockNumber = sp2[0]; + var previousBlockNumber = parseInt(sp2[0]); if(blockNumber <= previousBlockNumber && !eraseIfAlreadyRecorded){ throw constants.ERROR.PEER.ALREADY_RECORDED; } @@ -373,20 +373,20 @@ function PeeringService(server, pair, dal) { let peers = yield dal.findAllPeersNEWUPBut([selfPubkey]); peers = _.shuffle(peers); for (let i = 0, len = peers.length; i < len; i++) { - var p = new Peer(peers[i]); + let p = new Peer(peers[i]); logger.info("Try with %s %s", p.getURL(), p.pubkey.substr(0, 6)); - let node = yield Q.nfcall(p.connect); - let okUP = yield processAscendingUntilNoBlock(p, node, current); - if (okUP) { - let remoteCurrent = yield Q.nfcall(node.blockchain.current); - // We check if our current block has changed due to ascending pulling - let nowCurrent = yield dal.getCurrentBlockOrNull(); - logger.debug("Remote #%s Local #%s", remoteCurrent.number, nowCurrent.number); - if (remoteCurrent.number != nowCurrent.number) { - yield processLastTen(p, node, nowCurrent); - } - } try { + let node = yield Q.nfcall(p.connect); + let okUP = yield processAscendingUntilNoBlock(p, node, current); + if (okUP) { + let remoteCurrent = yield Q.nfcall(node.blockchain.current); + // We check if our current block has changed due to ascending pulling + let nowCurrent = yield dal.getCurrentBlockOrNull(); + logger.debug("Remote #%s Local #%s", remoteCurrent.number, nowCurrent.number); + if (remoteCurrent.number != nowCurrent.number) { + yield processLastTen(p, node, nowCurrent); + } + } // Try to fork as a final treatment let nowCurrent = yield dal.getCurrentBlockOrNull(); yield server.BlockchainService.tryToFork(nowCurrent); diff --git a/test/integration/peerings.js b/test/integration/peerings.js index 50b9aae0c32a849af2d3c77e10a917e231b7b463..f4cb542c9ff730dc853a6a93599f8c0d6e85dcca 100644 --- a/test/integration/peerings.js +++ b/test/integration/peerings.js @@ -1,5 +1,6 @@ "use strict"; +var co = require('co'); var Q = require('q'); var _ = require('underscore'); var should = require('should'); @@ -102,106 +103,60 @@ describe("Network", function() { }, Q()) .then(function(){ - nodeS1 = vucoin_p('127.0.0.1', s1.conf.port); - nodeS2 = vucoin_p('127.0.0.1', s2.conf.port); - nodeS3 = vucoin_p('127.0.0.1', s3.conf.port); - // Server 1 - return Q() - .then(function() { - return cat.selfCertPromise(now); - }) - .then(function() { - return toc.selfCertPromise(now); - }) - .then(function() { - return tic.selfCertPromise(now); - }) - .then(_.partial(toc.certPromise, cat)) - .then(_.partial(cat.certPromise, toc)) - .then(_.partial(cat.certPromise, tic)) - .then(cat.joinPromise) - .then(toc.joinPromise) - .then(tic.joinPromise) - .then(commitS1); - }) - - .then(function(){ - return Q() - .then(function(){ - // Server 2 syncs block 0 - return sync(0, 0, s1, s2); - }) - .then(function(){ - // Server 3 syncs block 0 - return sync(0, 0, s1, s3); - }) - .then(function(){ - return nodeS1.getPeer().then(function(peer) { - return nodeS2.postPeer(new Peer(peer).getRawSigned()); - }); - }) - .then(function(){ - return nodeS2.getPeer().then(function(peer) { - return nodeS1.postPeer(new Peer(peer).getRawSigned()); - }); - }) - .then(function(){ - return nodeS3.getPeer().then(function(peer) { - return nodeS1.postPeer(new Peer(peer).getRawSigned()); - }); - }) - .then(commitS1) - .then(function(){ - return Q.all([ - until(s2, 'block', 1), - until(s3, 'block', 1) - ]); - }) - .then(function(){ - // A block was successfully spread accross the network - s2.bma.closeConnections(); - }) - .then(commitS1) - .then(function(){ - return Q.all([ - until(s3, 'block', 1) - ]); - }) - .then(function(){ - s2.bma.reopenConnections(); - // Server 2 syncs block number 2 (it did not have it) - return sync(2, 2, s1, s2); - }) - .then(function(){ - return s2.recomputeSelfPeer(); - }) - .then(function(){ - return nodeS2.getPeer().then(function(peer) { - return nodeS1.postPeer(new Peer(peer).getRawSigned()); - }); - }) - .then(function(){ - return Q.all([ - until(s2, 'block', 2), - until(s3, 'block', 2), - commitS1() - .then(commitS1) - ]); - }) - .then(commitS3) - .then(function(){ - return Q.all([ - until(s1, 'block', 1), - until(s2, 'block', 1) - ]); - }) - .then(commitS2) - .then(function(){ - return Q.all([ - until(s1, 'block', 1), - until(s3, 'block', 1) - ]); - }); + return co(function *() { + nodeS1 = vucoin_p('127.0.0.1', s1.conf.port); + nodeS2 = vucoin_p('127.0.0.1', s2.conf.port); + nodeS3 = vucoin_p('127.0.0.1', s3.conf.port); + // Server 1 + yield cat.selfCertPromise(now); + yield toc.selfCertPromise(now); + yield tic.selfCertPromise(now); + yield toc.certPromise(cat); + yield cat.certPromise(toc); + yield cat.certPromise(tic); + yield cat.joinPromise(); + yield toc.joinPromise(); + yield tic.joinPromise(); + yield commitS1(); + // Server 2 syncs block 0 + yield sync(0, 0, s1, s2); + // Server 3 syncs block 0 + yield sync(0, 0, s1, s3); + yield nodeS1.getPeer().then((peer) => nodeS2.postPeer(new Peer(peer).getRawSigned())); + yield nodeS2.getPeer().then((peer) => nodeS1.postPeer(new Peer(peer).getRawSigned())); + yield nodeS3.getPeer().then((peer) => nodeS1.postPeer(new Peer(peer).getRawSigned())); + yield commitS1(); + yield [ + until(s2, 'block', 1), + until(s3, 'block', 1) + ]; + // A block was successfully spread accross the network + s2.bma.closeConnections(); + yield commitS1(); + yield [ + until(s3, 'block', 1) + ]; + s2.bma.reopenConnections(); + // Server 2 syncs block number 2 (it did not have it) + yield sync(2, 2, s1, s2); + yield s2.recomputeSelfPeer(); + yield [ + until(s2, 'block', 2), + until(s3, 'block', 2), + commitS1() + .then(commitS1) + ]; + yield commitS3(); + yield [ + until(s1, 'block', 1), + until(s2, 'block', 1) + ]; + yield commitS2(); + yield [ + until(s1, 'block', 1), + until(s3, 'block', 1) + ]; + }); }) ; });