Skip to content
This repository has been archived by the owner on Jun 7, 2019. It is now read-only.

Commit

Permalink
Merge pull request #115 from LiskHQ/96-improve-peers-selection
Browse files Browse the repository at this point in the history
Improve peers selection - Fixes #96
  • Loading branch information
karmacoma committed Apr 27, 2017
2 parents 45d50c9 + 159f887 commit fb880c0
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 14 deletions.
78 changes: 65 additions & 13 deletions lib/api/liskApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,40 +72,56 @@ 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',
'os': 'lisk-js-api',
'version': '1.0.0',
'minVersion': '>=0.5.0',
'port': this.port
};
} else {
NetHash = {
},
mainnet: {
'Content-Type': 'application/json',
'nethash': 'ed14889723f24ecc54871d058d98ce91ff2f973192075c0155ba2b7b70ad2511',
'broadhash': 'ed14889723f24ecc54871d058d98ce91ff2f973192075c0155ba2b7b70ad2511',
'os': 'lisk-js-api',
'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;
Expand Down Expand Up @@ -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();
}
};

Expand Down Expand Up @@ -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;
};

/**
Expand Down
42 changes: 41 additions & 1 deletion test/api/liskApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down Expand Up @@ -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();
});
Expand Down Expand Up @@ -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 () {
Expand Down

0 comments on commit fb880c0

Please sign in to comment.