diff --git a/lib/api/liskApi.js b/lib/api/liskApi.js index cff5fed57..143d4ead0 100644 --- a/lib/api/liskApi.js +++ b/lib/api/liskApi.js @@ -72,21 +72,27 @@ function LiskAPI (options) { this.options = options; this.ssl = options.ssl || false; - this.randomPeer = (typeof options.randomPeer === 'boolean') ? options.randomPeer : true; + //random peer can be set by settings with randomPeer: true | false + //random peer is automatically enabled when no options.node has been entered. Else will be set to false + //if the desired behaviour is to have an own node and automatic peer discovery, randomPeer should be set to true explicitly + this.randomPeer = (typeof options.randomPeer === 'boolean') ? options.randomPeer : !(options.node); this.testnet = options.testnet || false; this.bannedPeers = []; this.currentPeer = options.node || this.selectNode(); - if (options.port === '' || options.port) this.port = options.port; - else this.port = 8000; + this.port = (options.port === '' || options.port) ? options.port : 8000; this.parseOfflineRequests = parseOfflineRequest; this.nethash = this.getNethash(options.nethash); } -LiskAPI.prototype.getNethash = function (providedNethash) { - var NetHash; +/** + * @method netHashOptions + * @return {object} + */ + +LiskAPI.prototype.netHashOptions = function () { - if (this.testnet) { - NetHash = { + return { + testnet: { 'Content-Type': 'application/json', 'nethash': 'da3ed6a45429278bac2666961289ca17ad86595d33b31037615d4b8e8f158bba', 'broadhash': 'da3ed6a45429278bac2666961289ca17ad86595d33b31037615d4b8e8f158bba', @@ -94,9 +100,8 @@ LiskAPI.prototype.getNethash = function (providedNethash) { 'version': '1.0.0', 'minVersion': '>=0.5.0', 'port': this.port - }; - } else { - NetHash = { + }, + mainnet: { 'Content-Type': 'application/json', 'nethash': 'ed14889723f24ecc54871d058d98ce91ff2f973192075c0155ba2b7b70ad2511', 'broadhash': 'ed14889723f24ecc54871d058d98ce91ff2f973192075c0155ba2b7b70ad2511', @@ -104,8 +109,19 @@ LiskAPI.prototype.getNethash = function (providedNethash) { 'version': '1.0.0', 'minVersion': '>=0.5.0', 'port': this.port - }; - } + } + }; + +}; + +/** + * @method getNethash + * @return {object} + */ + +LiskAPI.prototype.getNethash = function (providedNethash) { + + var NetHash = (this.testnet) ? this.netHashOptions().testnet : this.netHashOptions().mainnet; if (providedNethash) { NetHash.nethash = providedNethash; @@ -152,6 +168,11 @@ LiskAPI.prototype.setTestnet = function (testnet) { this.bannedPeers = []; this.port = 7000; this.selectNode(); + } else { + this.testnet = false; + this.bannedPeers = []; + this.port = 8000; + this.selectNode(); } }; @@ -253,7 +274,38 @@ LiskAPI.prototype.checkReDial = function () { var peers = (this.ssl) ? this.defaultSSLPeers : this.defaultPeers; if (this.testnet) peers = this.defaultTestnetPeers; - return (peers.length !== this.bannedPeers.length); + var reconnect = true; + + //randomPeer discovery explicitly set + if(this.randomPeer === true) { + + //a nethash has been set by the user. This influences internal redirection + if(this.options.nethash) { + + //nethash is equal to testnet nethash, we can proceed to get testnet peers + if(this.options.nethash === this.netHashOptions().testnet.nethash) { + this.setTestnet(true); + reconnect = true; + //nethash is equal to mainnet nethash, we can proceed to get mainnet peers + } else if(this.options.nethash === this.netHashOptions().mainnet.nethash) { + this.setTestnet(false); + reconnect = true; + //nethash is neither mainnet nor testnet, do not proceed to get peers + } else { + reconnect = false; + } + + //No nethash set, we can take the usual approach, just when there are not-banned peers, take one + } else { + reconnect = (peers.length !== this.bannedPeers.length); + } + + //randomPeer is not explicitly set, no peer discovery + } else { + reconnect = false; + } + + return reconnect; }; /** diff --git a/test/api/liskApi.js b/test/api/liskApi.js index 07b559a87..9b66e39c9 100644 --- a/test/api/liskApi.js +++ b/test/api/liskApi.js @@ -91,6 +91,13 @@ describe('Lisk.api()', function () { (LISK.testnet).should.be.true; }); + + it('should set to mainnet', function () { + var LISK = lisk.api(); + LISK.setTestnet(false); + + (LISK.testnet).should.be.false; + }); }); describe('#setNode', function () { @@ -644,7 +651,7 @@ describe('Lisk.api()', function () { }); it('should be able to get a new node when current one is not reachable', function (done) { - lisk.api({ node: '123' }).sendRequest('blocks/getHeight', {}, function (result) { + lisk.api({ node: '123', randomPeer: true }).sendRequest('blocks/getHeight', {}, function (result) { (result).should.be.type('object'); done(); }); @@ -681,6 +688,39 @@ describe('Lisk.api()', function () { done(); }); }); + + it('should redial to new node when randomPeer is set true', function (done) { + var thisLSK = lisk.api({ randomPeer: true, node: '123' }); + thisLSK.getAccount('12731041415715717263L', function (data) { + (data).should.be.ok; + (data.success).should.be.equal(true); + done(); + }); + }); + + it('should not redial to new node when randomPeer is set to true but unknown nethash provided', function () { + var thisLSK = lisk.api({ randomPeer: true, node: '123', nethash: '123' }); + (thisLSK.checkReDial()).should.be.equal(false); + + }); + + it('should redial to mainnet nodes when nethash is set and randomPeer is true', function () { + var thisLSK = lisk.api({ randomPeer: true, node: '123', nethash: 'ed14889723f24ecc54871d058d98ce91ff2f973192075c0155ba2b7b70ad2511' }); + (thisLSK.checkReDial()).should.be.equal(true); + (thisLSK.testnet).should.be.equal(false); + }); + + it('should redial to testnet nodes when nethash is set and randomPeer is true', function () { + var thisLSK = lisk.api({ randomPeer: true, node: '123', nethash: 'da3ed6a45429278bac2666961289ca17ad86595d33b31037615d4b8e8f158bba' }); + (thisLSK.checkReDial()).should.be.equal(true); + (thisLSK.testnet).should.be.equal(true); + }); + + it('should not redial when randomPeer is set false', function () { + var thisLSK = lisk.api({ randomPeer: false}); + (thisLSK.checkReDial()).should.be.equal(false); + }); + }); describe('#sendRequest with promise', function () {